Search code examples
c++qtsignals-slotsqtreewidget

How to simulate a QTreeWidget itemClicked signal without making a derived class?


I am unable to find a proper simulation for ItemClicked() SIGNAL for QTreeWidget.

Is there a way to simulate it so that ItemClicked Signal is generated ?

e.g: we can emit ItemClicked in a derived class of QTreeWidget but cannot (as a QT rule) outside of it.


Solution

  • You can't use the emit call for class A to emit class B's signals. But note that the documentation for signals and slots says:

    "You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you need. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)"

    So you can work around this by declaring a signal in class A of the same signature as the one you want class B to emit, and connecting the signals together:

    connect(
        myclass, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
        treewidget, SIGNAL(itemClicked(QTreeWidgetItem*, int))
    ); 
    

    Then emit itemClicked from myclass. If I'm not mistaken, it will work for this case...and fire the treewidget's itemClicked signal for you.