0
votes

I do have a connection related to SIGNAL/SLOT use in Qt/C++

I have wrote an app using a QTreeWidget + QTreeWidgetItems. For the need of the app, I had to subclass QTreeWidgetItem to MyQTreeWidgetItem in order to add some parameters.

Since I move from QTreeWidgetItem class to MyQTreeWidgetItem class,

connect(this, SIGNAL(itemExpanded(MyQTreeWidgetItem*)),
                 this, SLOT(onSubTreeDisplay(MyQTreeWidgetItem*)));

is not working anymore.

The issue is

QObject::connect: No such signal PulsTreeWidget::itemExpanded(MyQTreeWidgetItem*) in ../puls_connect/pulstreewidget.cpp:33

I think that the issue is coming from the fact that itemExpanded expect QTreeWidgetItem and not MyQTreeWidgetItem. But if I replace MyQTreeWidgetItem by QTreeWidgetItem such as

connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)),
                 this, SLOT(onSubTreeDisplay(MyQTreeWidgetItem*)));

the run complain that SIGNAL/SLOT do not have the same type and this normal.

My item is defined through MyQTreeWidgetItem and not QTreeWidgetItem as I have subclass it.

The connect is done in the QTreeWidget part Any idea ?

thanks

1

1 Answers

1
votes

You can do the signal slot connection like below

connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(onSubTreeDisplay(QTreeWidgetItem*)));

Then inside onSubTreeDisplay(), dynamic cast QTreeWidgetItem argument to MyQTreeWidgetItem like below

void onSubTreeDisplay(QTreeWidgetItem* item)
{
    MyTreeWidgetItem* myItem = dynamic_cast<MyTreeWidgetItem*>(item);
    if (myItem)
    {
        //cast is successful. you can use myItem
    }
}