Qt C++:使用 QMap 将多个字符串保存在 QListWidgetItem 中
我正在尝试在一个 QListWidgetItem 中保存多个字符串。我知道,您可以在 Qt::UserRole 中保存一个 String/QVariant,但我想在其中保存多个字符串。
我尝试在其中保存 Map,但无法将 QMap 转换为 QVariant,因此我尝试在其中保存指针,这可行,但我无法从 QVariant 检索指针。
使用 QMap 在 Qt::UserRole 中保存多个 QString 的最佳方法是什么?
谢谢
我的尝试(直接保存 QMap 是不可能的,因为它无法转换为 QVariant):
创建指向 QMap 的指针:
//create Map to store more values in UserRole and getting value from dialog inputs
QMap<QString, QString> dataMap;
dataMap["Model"] = dialog.carModelEditTEXT();
dataMap["Year"] = dialog.carYearEditTEXT();
dataMap["Value"] = dialog.carValueEditTEXT();
QMap<QString, QString> * mapPointer = &dataMap;
//converting pointer to QVariant ot store it in User Role
QVariant userRoleData;
userRoleData.setValue(mapPointer);
尝试从 Qt::QVariant 加载指针,然后从中创建一个 Map:
//get Map from User Roel through pointer
QVariant userRoleVariant = curItem->data(Qt::UserRole);
QMap<QString, QString>* userRoleMap = userRoleVariant.value<QMap<QString, QString>>();
QMap<QString, QString> dataMap = &userRoleMap;
I am trying to save multiple string within one QListWidgetItem. I know, that you can save one String/QVariant in Qt::UserRole, but I want to save multiple strings in it.
I tried to save an Map in it, but I can´t convert QMap to QVariant, so I tried to save a pointer in it, which works, but I´m not able to retrieve the pointer from the QVariant.
Which is the best way to save multiple QStrings in Qt::UserRole with a QMap?
Thanks
My Attempt(Directly saving QMap isn´t possible, as it can´t be converted to QVariant):
Creating pointer to QMap:
//create Map to store more values in UserRole and getting value from dialog inputs
QMap<QString, QString> dataMap;
dataMap["Model"] = dialog.carModelEditTEXT();
dataMap["Year"] = dialog.carYearEditTEXT();
dataMap["Value"] = dialog.carValueEditTEXT();
QMap<QString, QString> * mapPointer = &dataMap;
//converting pointer to QVariant ot store it in User Role
QVariant userRoleData;
userRoleData.setValue(mapPointer);
Trying to load pointer from Qt::QVariant and then create a Map from it:
//get Map from User Roel through pointer
QVariant userRoleVariant = curItem->data(Qt::UserRole);
QMap<QString, QString>* userRoleMap = userRoleVariant.value<QMap<QString, QString>>();
QMap<QString, QString> dataMap = &userRoleMap;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论