Important: Please read the Qt Code of Conduct -

[SOLVED]about get the Index of the SelectedItem on the QTreeWidget

  • there is no way to get the index directly from the SelectedItem?
    i use this way: pSelItemParent->indexOfChild(pSelItem);
    is this the only way?

  • currentIndex() ?

  • thank you. i'll have a try
    is it equal to the selectedItem's index?

  • If only one item is selected, I think yes, because setting current item also selects it.

    Well, I think you will get problems when having more than one selected item. I think the only way to handle with that is using selectedItems and IndexFromItem:
    @QTreeWidget* treewidget;

    // some code...

    QList<QTreeWidgetItem*> items_selected = treewidget->selectedItems();
    QList<QModelIndex*> indexes_selected;
    for(int i = 0; i < items_selected.count(); i++)
    (not tested)

    or directly using:
    I would also use this method if only one item is selected, you can check that with items_selected.count() if you need this information later. currentItem() is no good way for multi-selections I think.

    Hope that helps you...

  • thank you very much :)

Log in to reply