继承QTreeWidgetItem发生error: 'staticMetaObject' is not a member of 'QTreeWidgetItem' 错误
#ifndef QQUSERITEM_H就发生下列错误
#define QQUSERITEM_H
#include <QTreeWidgetItem>
class QQUserItem :public QTreeWidgetItem
{
Q_OBJECT
public:
explicit QQUserItem(QQUserItem *parent = 0);
signals:
public slots:
};
#endif // QQUSERITEM_H
debug\moc_QQUserItem.cpp:41:8: error: 'staticMetaObject' is not a member of 'QTreeWidgetItem'
..\..\Qt\4.6.3\include/QtCore/../../src/corelib/kernel/qobject.h: In member function 'virtual const QMetaObject* QQUserItem::metaObject() const':
..\..\Qt\4.6.3\include/QtCore/../../src/corelib/kernel/qobject.h:296:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
debug\moc_QQUserItem.cpp:51:21: error: within this context
..\..\Qt\4.6.3\include/QtCore/../../src/corelib/kernel/qobject.h:296:33: error: object missing in reference to 'QObject::d_ptr'
debug\moc_QQUserItem.cpp:51:21: error: from this location
..\..\Qt\4.6.3\include/QtCore/../../src/corelib/kernel/qobject.h:296:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
debug\moc_QQUserItem.cpp:51:50: error: within this context
..\..\Qt\4.6.3\include/QtCore/../../src/corelib/kernel/qobject.h:296:33: error: object missing in reference to 'QObject::d_ptr'
debug\moc_QQUserItem.cpp:51:50: error: from this location
debug\moc_QQUserItem.cpp: In member function 'virtual void* QQUserItem::qt_metacast(const char*)':
debug\moc_QQUserItem.cpp:59:12: error: 'qt_metacast' is not a member of 'QTreeWidgetItem'
debug\moc_QQUserItem.cpp: In member function 'virtual int QQUserItem::qt_metacall(QMetaObject::Call, int, void**)':
debug\moc_QQUserItem.cpp:64:11: error: 'qt_metacall' is not a member of 'QTreeWidgetItem'
debug\moc_QQUserItem.cpp: In member function 'virtual void* QQUserItem::qt_metacast(const char*)':
debug\moc_QQUserItem.cpp:60:1: warning: control reaches end of non-void function
debug\moc_QQUserItem.cpp: In member function 'virtual const QMetaObject* QQUserItem::metaObject() const':
debug\moc_QQUserItem.cpp:52:1: warning: control reaches end of non-void function
mingw32-make[1]: *** [debug/moc_QQUserItem.o] Error 1
mingw32-make: *** [debug] Error 2
The process "D:/MinGW/bin/mingw32-make.exe" exited with code %2.
Error while building project MyQQ (target: Desktop)
When executing build step 'Make'
修改成这样 class QQUserItem :public QObject,public QTreeWidgetItem就没错误了,搜到了如下资料:
居然有这样的错误:'staticMetaObject' is not a member of 'Ui::MainWindow'
我们无法使用 template 继承 QObject,换言之,下面代码无法被 moc 转换成为有效的 C++ compiler 可编译代码:
使用 multiple inheritance 必须把 QObject 或者其子类放在第一个父类的位置(因为使用 Q_OBJECT 需要覆盖),然后继承别的类。颠倒后,如 g++ 会抱错,如
函数指针不能作为 signal/slot 的参数,这个可以用 typedef 克服,如
class SomeClass : public QObject
{
public slots:
}; 将被认为非法(主要是判断参数类型时会失败,记得 QMetaObject 存下来的是什么信息么?),但是
typedef void (*ApplyFunctionType)( List *, void * );
class SomeClass : public QObject
{
public slots:
}; 是可行的。
友 元声明最好不要放在 signal 和 slot 声明中,这很明显,因为多数情况下虽然根据宏替换, signals: 被替换为 protected:,slots 被替换为空,但是编译器处理 friend 可能并不完全这样无关的处理,理论上说标准的编译器应该能 work,如 g++ 4.3.3.
signal 和 slot 不能被 upgrade,这是因为 moc 需要一个完整的函数声明,而提升的时候声明是不完整的,如
class SlotClass : public QObject
{
protected slots:
} ;
class UpgradeSlotClass : public SlotClass
{
public slots:
} ;
/usr/bin/moc-qt4 -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. upgradeslotclass.hpp -o moc_upgradeslotclass.cpp
upgradeslotclass.hpp:15: Error: Not a signal or slot declarationsignal 和 slot 的参数不能使用宏,这是因为 moc 不展开宏。
嵌套类声明不应该出现在 signal 或者 slot 里面,也是因为 moc 不能处理的原因。
构造函数不应出现在 signal 和 slot 里面,虽然是函数,但是没有必要。
Q_PROPERTY 宏声明属性时应在包含其读写函数的 public 节之前,写在同一个 public 里面不允许,注意
#define Q_PROPERTY(text)其实在类声明时这句话没有任何作用,只是 moc 编译会产生对应的代码,而 moc 要求这部分必须以类似下面的方式书写
class PropertyClass : public QObject
{