如何从QVariant中检索QPair?

时间:2017-06-11 07:09:27

标签: qt

我正在做auto data = combobox->currentData().value<QPair>();,但编译器抱怨:

[ 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

据我所知,我的电话与template<class T> T QVariant::value()排成一行 - 出了什么问题?

1 个答案:

答案 0 :(得分:3)

QPair是一个模板类,您从变体中获取值的代码并未完全描述该类型。

首先,您需要了解QPair描述的两种类型。然后,您必须使用以下代码将其解压缩(将QString和int更改为您的对数据类型):

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