Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get clicked/selected items of a QTreeWidget

Tags:

c++

qt

I'm currently looking to a way to get the name of the selected items of a QTreeWidget.

I have create multiple QTreeWidgetItems to generate a file browser-like.

I need to know how to get the name of the folder selected.

I have found the

this->MyTree->selectedItems();

to get it but I'm not able to store the feedback which is supposed to be a QList format.

Any examples on how to store the selectedItems list ?

like image 892
Seb Avatar asked Sep 29 '22 09:09

Seb


1 Answers

From the Qt documentation: QTreeWidget Class Reference , QTreeWidgetItem Class Reference

selectedItems() is a function of QTreeWidget.

QList QTreeWidget::selectedItems () const

Returns a list of all selected non-hidden items.

text() is a function of QTreeWidgetItem

QString QTreeWidgetItem::text ( int column ) const

Returns the text in the specified column.

Define a list of QTreeWidgetItem to store return value of selectedItems() .
For each item in the list use text() function to get it's string.

QList<QTreeWidgetItem *> itemList;
itemList = this->MyTree->selectedItems();
foreach(QTreeWidgetItem *item, itemList)
{
   QString str = item->text();
   //str is what you want
}
like image 160
Ali Mofrad Avatar answered Oct 29 '22 16:10

Ali Mofrad