Qt C++:使用 QMap 将多个字符串保存在 QListWidgetItem 中

发布于 2025-01-15 22:25:56 字数 1214 浏览 3 评论 0原文

我正在尝试在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文