QTreeWidget: figure out all possible celection methods

  • Hello there,
    In order to find the index (int) of the item selected by click in the QTreeWidget I used itemClicked signal. I have a problem however. I noticed that user can select an item, and with the left button clicked swing through the items in the view. The item that ones unclicks the mouse at becomes selected. But in that case no signal is generated, at least I cannot figure out which one. I would really appreciate all help in this matter.

  • Lifetime Qt Champion


    You should rather use the QItemSelectionModel::selectionChanged signal from your QTreeWidget selection model.

  • But is it possible to retrieve selected column and text from this model? For clicked item i get a QTreeWidgetItem, so I do:

     * \brief CItemsMenu::on_twItems_itemClicked: Find index of selected item in this slot
     * \param item
     * \param column
    void CItemsMenu::on_twItems_itemClicked(QTreeWidgetItem *item, int column)
        // get selected item text
        QString itemText = item->text(column);
        QTreeWidgetItemIterator it(mp_tw);
        int index = 0;
        while (*it)
            if ((*it)->text(column) == itemText)
                // item found, update global index
                m_currentItem = index;
                        //<< "Item index changed by click: " << m_currentItem;
                //<< "Item index not found by click!";

  • Lifetime Qt Champion

    You get the indexes matching selection/deselection from there you can get the items from the tree.

Log in to reply