我无法理解 Q_OBJECT 子类的 QVariant 的行为(GVPalette* 继承自具有 Q_OBJECT 宏的 BasicItemNode)。编译器错误是
/qobject.h:471: instantiated from ‘T qobject_cast(QObject*) [with T = GVPalette*]’
/qvariant.h:584: instantiated from ‘static T QtPrivate::QVariantValueHelper<T>::object(const QVariant&) [with T = GVPalette*]’
/qvariant.h:100: instantiated from ‘static ReturnType QtPrivate::ObjectInvoker<Derived, Argument, ReturnType>::invok
e(Argument) [with Derived = QtPrivate::QVariantValueHelper<GVPalette*>,
Argument = const QVariant&, ReturnType = GVPalette*]’
/qvariant.h:593: instantiated from ‘T qvariant_cast(const QVariant&) [with T = GVPalette*]’
/CLSMetaType.cpp:50: instantiated from here
/BasicItemNode.h:27: error: void value not ignored as it ought to be
下面代码段中的第 2 行对应于 CLSMetaType.cpp:50 和第 6 行。对应于 BasicItemNode.h:27
CLSMetaType.cpp
if(variant.canConvert<GVPalette*>()) {
1.//retVal = variant.value<GVPalette*>()->objectName();
2. GVPalette* gv = variant.value<GVPalette*>();
3. retVal = gv->objectName();
}
BasicItemNode.h
4. class BasicItemNode : public QObject
5. {
6. Q_OBJECT
Qt5 - 开源版本 g++ - 4.6.3