Vadim Peretokin
Vadim Peretokin

Reputation: 2826

How to retrieve QPair from QVariant?

I'm doing auto data = combobox->currentData().value<QPair>(); but the compiler complains with:

[ 48%] Building CXX object src/CMakeFiles/mudlet.dir/dlgProfilePreferences.cpp.o
/home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp: In lambda function:
/home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp:420:81: error: no matching function for call to ‘QVariant::value()’
                 auto data = script_preview_combobox->currentData().value<QPair>();
                                                                                 ^
In file included from /home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/QVariant:1:0,
                 from /home/vadi/Programs/Mudlet/mudlet/cmake-build-debug/src/ui_profile_preferences.h:12,
                 from /home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.h:27,
                 from /home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp:25:
/home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/qvariant.h:351:14: note: candidate: template<class T> T QVariant::value() const
     inline T value() const
              ^
/home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/qvariant.h:351:14: note:   template argument deduction/substitution failed:
src/CMakeFiles/mudlet.dir/build.make:806: recipe for target 'src/CMakeFiles/mudlet.dir/dlgProfilePreferences.cpp.o' failed

As far as I see, my call is lining up with template<class T> T QVariant::value() - what's wrong?

Upvotes: 1

Views: 1303

Answers (1)

aatwo
aatwo

Reputation: 1008

QPair is a template class and your code for getting the value from the variant does not fully describe the type.

First you need to know what two types your QPair describes. Then you must use the following code to extract it (changing the QString and int to your pairs data types):

auto pair = combobox->currentData().value<QPair<QString, int> >();

Upvotes: 5

Related Questions