Qt World Summit: Register Today!

[SOLVED] Emiting QTreeWidget currentItemChanged event

  • Hi,

    I have a class named MyClass, inside that there are two objects - QTreeWidget, and QTableWidget. When an item in QTableWidget is double clicked, I need to fire a QTreeWidget "currentItemChanged" signal. My pseudo code looks like this...


    void Myclass::TreeItemSelected(QTreeWidgetItem*)
    //do something

    void Myclass::TableItemDoubleClicked(int r,int c)
    //emit currentItemChanged signal of the MyTreeObject

    Whats the right method to emit the signal in this case?

    Thanks Lloyd

  • You cannot emit signals from outside a class[1]. What you can do, is sublcass the QTreeWidget, then, in MyClass, connect the table widget's cellDoubleCliced signal to that slot and have that slot emit the currentItemChanged signal.

    fn1. In fact, the "signals" pseudo keyword is just a macro and eventually translates to "private".

  • Thanks Volker, that solved my problem partially.

    I was expecting that, if emit a "currentItemChanged" event, the selection in the tree control would also change. But that didn't happen!

    This arises a question in mind that, is there any use for "emit" ?, without that also I can accomplish the task by a simple function call


  • No, that doesn't work, as it is just the other way round: A signal is fired when something happened - e.g. the current item changed and the currentItemChanged() signal is emitted. If you want something to happen, you call a slot. In your case, you can call setCurrentItem() on the tree view.

    The use of emit is in your own classes. In case something interesting happened, you emit a signal. Other classes connect to it and behave accordingly.

  • Thanks Volker, now I am clear :)

Log in to reply