Qt C++: Save multiple strings in a QListWidgetItem with a QMap

249 Views Asked by At

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;
0

There are 0 best solutions below