Gaurav Kumar
Gaurav Kumar

Reputation: 9

How do I invoke a SLOT on expansion QTreeWidgetItem and not QTreeWidget?

I have my class in TaskGenJob.h where TaskGenModel inherits from QObject:-

TaskGenJob.h

class TaskGenJob : public TaskGenModel    
{
    Q_OBJECT    
public:    
    TaskGenJob();    
    ~TaskGenJob();
}

TaskGenJob::TaskGenJob()
{    
//Connect signal itemExpanded to updateValue so that Display is updated only on tree expansion

connect(TaskGenJobDisplayPtr->getTaskGenJobTreeWdiget(), SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(updateValue()));    
}

In my TaskGenJob.cpp file, I try to connect QTreeWidgetItem's Signal to updateValue like the above.

But I get the following errors:-

/opt/qt_4.8.6/include/linux60_64/gcc48/QtCore/qobject.h:204:17: note:   no known conversion for argument 1 from 'QTreeWidgetItem*' to 'const QObject*'
/opt/qt_4.8.6/include/linux60_64/gcc48/QtCore/qobject.h:217:17: note: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
     static bool connect(const QObject *sender, const QMetaMethod &signal,
                 ^
/opt/qt_4.8.6/include/linux60_64/gcc48/QtCore/qobject.h:217:17: note:   no known conversion for argument 1 from 'QTreeWidgetItem*' to 'const QObject*'
/opt/qt_4.8.6/include/linux60_64/gcc48/QtCore/qobject.h:337:13: note: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
 inline bool QObject::connect(const QObject *asender, const char *asignal,

The errors are because QTreeWidgetItem doesn't inherit from QObject and doesn't have SIGNAL(itemExpanded(QTreeWidgetItem*)). In my application's GUI I have 1 QTreeWideget and thousands of QTreeWidgetItem children. I have access to the pointers to these children. The requirement is that I must call the SLOT updateValue only on expansion of QTreeWidgetItem children. How can I achieve this?

Upvotes: 0

Views: 434

Answers (1)

Ian4264
Ian4264

Reputation: 327

As the error says, QTreeWidgetItem is not a subclass of Q object, nor does it have any signals. It is QTreeWidget that has these, so try connecting up it instead, in the same way as you are doing in the code snippet.

The QTreeWidget::itemExpanded signal is emitted when any of it's items are expanded and which one is expanded is in it's argument.

Upvotes: 2

Related Questions