Unsolved How to get selected items in tree widget
-
@suslucoder it is work
QList<QTreeWidgetItem*> slctdList = ui->treeWidget2->selectedItems(); for(QTreeWidgetItem* item : slctdList) { scln = QString::fromStdU16String(item->text(0).toStdU16String()); qDebug() << scln; }
-
@suslucoder I have another question,
in table widget i can restrict the selected rows and do my operation like
if(item->row() >=0 & item->row() <=10) { ........ }
How should it be in tree widget?
There is no row -
-
@jsulm i can get the index of my double clicked items like that
qDebug() << ui->treeWidget2->currentIndex().row();
but how can adapt it for selected ones
-
@suslucoder
@jsulm just referred you toQTreeView::selectedIndexes()
, which returns aQModelIndexList
, and toQModelIndex
, which has arow()
member (as you use fromcurrentIndex()
). Can you not put those two together?for (QModelIndex index : ui->treeWidget2->selectedIndexes()) qDebug() << index.row();
Or do you have some other question?
-
@JonB but tree widget doesnt have selectedIndexes()
-
@suslucoder
But it does haveselectedIndexes
. @jsulm even gave you the reference. Go to List of All Members for QTreeWidget, search forselectedIndexes
. Please remember about C++ inheritance when you are programming. -
@JonB i remember but maybe qt dont.
Because it gives me that error -
i remember but maybe qt dont.
So Qt/C++ inheritance does not work, and the documentation is wrong, but you are correct?
Because it gives me that error
What error? You have not shown any error. Again, how many times can we ask you: if you get an error, paste the line of code and paste the error message you receive. Could you please do this each time instead of saying "something does not work"?
EDIT
Ah, now hang on. I have just noticed thatQModelIndexList QTreeView::selectedIndexes() const
is documented[override virtual protected]
. All the more reason to show the error message instead of saying "Because it gives me that error"....Because of the
protected
, that means you will not be able to accessui->treeWidget2->selectedIndexes()
unless you have subclassedtreeWidget2
from the baseQTreeWidget
, which I do not imagine you have done. So instead you should useui->treeWidget2->selectionModel()
to access the selections. ThatQItemSelectionModel
has methodsselectedIndexes()
or evenselectedRows()
to access the selected rows directly, which is what you asked for:qDebug() << ui->treeWidget2->selectionModel()->selectedRows()
. -
@JonB Ok. Anyway. I did it
if(item->parent() == myparentitem) { ....}
-
@JonB said in How to get selected items in tree widget:
instead of saying "Because it gives me that error"....
You have to ask some people again and again to provide basic information...