Frage

I am getting a compilation error for the following code. I cannot use signals with tree items:

class MyClass : public QObject
{
    Q_OBJECT

public:
    int Grow();

public slots:
    void AddSubtree(QTreeWidgetItem *_jobItem, int _column);

private:
    void AddSubtree(QTreeWidgetItem *_jobItem, const QString &_filePath);
};

int MyClass::Grow()
{
    ...
    foreach(QFileInfo fileInfo, filesList)
    {
        if(fileInfo.isDir())
        {
            const QString tag = fileInfo.fileName();

            QTreeWidgetItem* item = new QTreeWidgetItem();
            item->setText(0, tag);

            // This works, but it is performance critical! Do it on demand
            //AddRunSubtree(item, fileInfo.filePath());

            // Load this tag's subtree on demand when clicked (error on this line)
            QObject::connect(item, SIGNAL(itemClicked(QTreeWidgetItem*, int col)),
                             this, SLOT(AddSubtree(QTreeWidgetItem*, int col)));

            parentItem->addChild(item);
        }
    }
}

void MyClass::AddSubtree(QTreeWidgetItem *_jobItem, int _column)
{
    //...
}

void MyClass::AddSubtree(QTreeWidgetItem *_jobItem, const QString &_filePath)
{
    //...
}

I am getting this compilation time error:

error: no matching function for call to 'MyClass::connect(QTreeWidgetItem*&, const char*, MyClass* const, const char*)'

Candidates are:

c:\Qt\4.8.5\src\corelib\kernel\qobject.h:204: static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)
c:\Qt\4.8.5\src\corelib\kernel\qobject.h:204: note:   no known conversion for argument 1 from 'QTreeWidgetItem*' to 'const QObject*'

c:\Qt\4.8.5\src\corelib\kernel\qobject.h:217: static bool QObject::connect(const QObject*, const QMetaMethod&, const QObject*, const QMetaMethod&, Qt::ConnectionType)
c:\Qt\4.8.5\src\corelib\kernel\qobject.h:217: note:   no known conversion for argument 1 from 'QTreeWidgetItem*' to 'const QObject*'

c:\Qt\4.8.5\src\corelib\kernel\qobject.h:337: bool QObject::connect(const QObject*, const char*, const char*, Qt::ConnectionType) const
c:\Qt\4.8.5\src\corelib\kernel\qobject.h:337: note:   no known conversion for argument 1 from 'QTreeWidgetItem*' to 'const QObject*'

Looking at the definition of QTreeWidgetItem, it effectively does not derive from QObject. Does it mean I cannot make the selection of a tree branch generate a signal?

If it is possible, what is wrong with my code?

Why does the error message contain 'MyClass::connect' instead of 'QObject::connect'?

Platform:
Qt 4.8.5
MinGW/g++
Windows 7

War es hilfreich?

Lösung 2

You are using the wrong class for the sender of the connection. You are using an item, whereas you should use the QTableWidget itself.

If you switch to that, and you set the connection once, your slot will be called.

Andere Tipps

QTreeWidgetItem does not inherit from QObject, so it means it doesn not support signals/slots.

void itemClicked(QTreeWidgetItem*, int) is signal from QTreeWidget, so you need to do it this way:

QObject::connect(treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(AddSubtree(QTreeWidgetItem*, int)));

Also small note - you need to create this connection only once (in constructor) not on every item addition.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top