QTreeWidget with LineEdit to filter and search element in my tree
-
@coilo
Hi.
Never done!
As the first idea I think:QList<QTreeWidgetItem*> list;
list = MyQTreeWidget->findChildren<QTreeWidgetItem*>();
Then foreach in the list to find text that contains your substring, then select QTreeWidgetItem
Unique doubt, I'm not sure that list has the correct order, you must check
-
Instead of using
QTreeWidgetsplit the model and the view (QStandardItemModel+QTreeView)
Then you can just use aQSortFilterProxyModelin-between to do the filtering -
@coilo
You are welcome.
Please, write the result of your test, so I know if it is a good way for future
Thanks@CP71
Yes of course no problem !
But i have some issues here, i am a beginner with Qt, i am trying your solution the function contains does not exist with QTreeWidgetItem.
Here is my code :void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
QList<QTreeWidgetItem*> list;
list= ui->treeWidget->findChildren<QTreeWidgetItem*>();foreach (QTreeWidgetItem* myitem,list) { if(myitem->contains(arg1)) { list.append(myitem); } }}
Thanks again ! =)
-
@CP71
Yes of course no problem !
But i have some issues here, i am a beginner with Qt, i am trying your solution the function contains does not exist with QTreeWidgetItem.
Here is my code :void MainWindow::on_lineEdit_textChanged(const QString &arg1)
{
QList<QTreeWidgetItem*> list;
list= ui->treeWidget->findChildren<QTreeWidgetItem*>();foreach (QTreeWidgetItem* myitem,list) { if(myitem->contains(arg1)) { list.append(myitem); } }}
Thanks again ! =)
-
Hi,
For the searching part, no need to complicate stuff, QTreeWidget has you covered with findItems.
-
@coilo said in QTreeWidget with LineEdit to filter and search element in my tree:
ui->treeWidget->clear(); //i would like to delete all element (but only the view not the data)You won't be able to do this. In a
QTreeWidgetit manages the data and the displayed items itself, you cannot have one without the other. As someone said earlier, the best way to really do this is with your ownQTreeViewplus aQSortFilterProxyModel, which would achieve what you want, but you have said you have reasons for not wanting to change over.If you are going to stick with your
QTreeWidgetyou will need to clear its current members and then re-add the desired new items fromsublistinto yourui->treeWidget, not just callui->treeWidget->setCurrentItem()(which only selects an existing item already in aQTreeWidget).Note also: you find items recursively, but you only produce a "flat"
sublist. If doing it this way you will need to code whatever to re-add the old items in the correct hierarchy/parentage. -
Disclaimer
This is not the advised solution, the correct one is to use
QStandardItemModel+QSortFilterProxyModel+QTreeView.
Since you are already hacking, might as well hack properly.
- Create a
QTreeViewright next to yourQTreeWidget. - Add a private
QSortFilterProxyModel* m_proxy;member toMainWindow - in the
MainWindowconstructor add:
m_proxy = new QSortFilterProxyModel(this); m_proxy->setSourceModel(ui->treeWidget->model()); ui->treeView->setModel(m_proxy); ui->treeWidget->hide();on_lineEdit_textChangedbecomes:
void MainWindow::on_lineEdit_textChanged(const QString &arg1) { m_proxy->setFilterRegularExpression(arg1); } - Create a