PDA

View Full Version : Can I subclass QTreeWidgetItem?



Shawn
2nd September 2007, 15:08
I tried this:

class MyTreeWidgetItem : public QTreeWidgetItem
{
Q_OBJECT
public:
MyTreeWidgetItem(QTreeWidgetItem *parent);
~MyTreeWidgetItem();
};

with these errors

.\debug\moc_mytreewidgetitem.cpp(37) : error C2039: 'staticMetaObject' : is not a member of 'QTreeWidgetItem'
e:\qt\4.2.2\include\qtgui\../../src/gui/itemviews/qtreewidget.h(41) : see declaration of 'QTreeWidgetItem'
.\debug\moc_mytreewidgetitem.cpp(51) : error C2039: 'qt_metacast' : is not a member of 'QTreeWidgetItem'
e:\qt\4.2.2\include\qtgui\../../src/gui/itemviews/qtreewidget.h(41) : see declaration of 'QTreeWidgetItem'
.\debug\moc_mytreewidgetitem.cpp(56) : error C2039: 'qt_metacall' : is not a member of 'QTreeWidgetItem'
e:\qt\4.2.2\include\qtgui\../../src/gui/itemviews/qtreewidget.h(41) : see declaration of 'QTreeWidgetItem'

I used to subclass the QGraphicsRectItem and it works well...:confused:
class MyRectItem : public QObject, public QGraphicsRectItem
{
Q_OBJECT

...
}

marcel
2nd September 2007, 15:12
You also have to add QObject first in the base list.
However, you only need the Q_OBJECT macro if you plan to add some signals/slots to this class.

Regards

Shawn
2nd September 2007, 15:39
You also have to add QObject first in the base list.
However, you only need the Q_OBJECT macro if you plan to add some signals/slots to this class.

Regards

Thanks very much for your immediate replay! Now it works.

Would you mind to tell me why I should inherite QObject first? Actually I used to add it in the base list right behind QTreeWidgetItem, still it can not work.

jpn
2nd September 2007, 16:35
It's a limitation of moc: Multiple Inheritance Requires QObject to Be First (http://doc.trolltech.com/latest/moc.html#multiple-inheritance-requires-qobject-to-be-first).

marcel
2nd September 2007, 16:37
This is required in multiple inheritance. It is a requirement of moc because it assumes the first base class is a QObject.

Regards.