Solved Search and select item in QTreeWidget based on Search String
-
Hi ,
I Already have this function about QLineEndit to write in the LineEdit then search in TreeWidget
void Profile3::on_lineEdit_textEdited(const QString &arg1) { QList<QTreeWidgetItem*> clist = ui->treeWidget_Medicine->findItems(arg1, Qt::MatchContains|Qt::MatchRecursive, 0); // QItemSelectionModel <QTreeWidgetItem*> select; for(QTreeWidgetItem* item: clist) { // select->setCurrentIndex(select,Qt::MatchContains|Qt::MatchRecursive); ui->treeWidget_Medicine->setCurrentItem(item,0); item->setBackground(0,Qt::red) ; qDebug() << endl << item->text(0) << "Medicine" ; } }
but it went this , so i just need to tell me about QItemSelectionModel that how to use with TreeWidget to select multiple chocies even if i made the loop to clear highlight
-
Hi
So it colored all items ??? -
Yes it colored all the items after i delete and rewrite in QLineEdit so i guess as you said it needs also for loop to delete the highlight but i guess it will not be elegent i just need multiple selection like (I mean the way of selection) of
-
ui->treeWidget_Medicine->setCurrentItem(item,0);
that when i search for something highlight the normal like
- setCurrentItem
Thanks in Advance appreciate
-
-
@MostafaEzzat
Hi
Ah yes if you dont clear the background before each search that will happen.Im not sure how we can do when its a TreeWidget as the function we need is protected
https://doc.qt.io/qt-5/qtreewidget.html#indexFromItem
so we could do
auto sel = ui->treeWidget->selectionModel(); for(QTreeWidgetItem* item: clist) { QModelIndex index = ui->treeWidget->indexFromItem(item,0); sel->select(index) ; }
So you would need to subclass your TreeWidget to do this.
-
I inherted QTreeWidget like this and still gives me it's protected or do you think i should rewrite QModel Index file to inhert QTreeWidgetItem in it
class Profile3 : public QWidget , public QTreeWidgetItem , public QModelIndex {
-
D:\New folder\Hospitaal\profile3.cpp:360: error: 'QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem*, int) const' is protected within this context
-
..\Hospitaal\profile3.cpp: In member function 'void Profile3::on_lineEdit_textEdited(const QString&)':
-
..\Hospitaal\profile3.cpp:360:71: error: 'QModelIndex QTreeWidget::indexFromItem(QTreeWidgetItem*, int) const' is protected within this context
-
QModelIndex index = ui->treeWidget_Medicine->indexFromItem(item,0);
-
D:\New folder\Hospitaal\profile3.cpp:361: error: no matching function for call to 'QItemSelectionModel::select(QModelIndex&)'
-
..\Hospitaal\profile3.cpp:361:23: error: no matching function for call to 'QItemSelectionModel::select(QModelIndex&)'
-
sel->select(index) ;
-
-
@MostafaEzzat
Hi
Ehh that is nothing like I expected. o.OYou need a subclassed TreeWidget like code below
and use that instead of the standard one.class MyTreeWidget : public QTreeWidget { Q_OBJECT public: explicit MyTreeWidget(QWidget *parent = nullptr) : QTreeWidget(parent) { } void DoSearch(QString term) { QList<QTreeWidgetItem *> clist = findItems(term,Qt::MatchContains | Qt::MatchRecursive, 0); auto sel = selectionModel(); for (QTreeWidgetItem *item : clist) { QModelIndex index = indexFromItem(item, 0); sel->select(index, QItemSelectionModel::Select) ; } } }; and use your new DoSearch ui->treeWidget->selectionModel()->clear(); // get rid of old selection ui->treeWidget->DoSearch(ui->lineEdit->text());
Then it will select them
Test Project
https://www.dropbox.com/s/zu5tc2hobdwt56i/justDefualt.zip?dl=0Do note. I used the promotion feature of Creator to replace the normal one with my subclassed one.
That is why you dont see code that goes
MyTreeWidget * tree = new MyTreeWidget(this);
To insert into main form. -
This post is deleted! -
This post is deleted! -
-
@MostafaEzzat
Good to hear :) -
Please Sir, it's almost a month I'm trying to know how to return a big text (10MB) in QTextWidget or in any suitable widget you would suject
(I'm trying to work on a similar project like esword, see here link text) -
@John-H-BROOKS
Hello and welcome.Open your own new thread with your question, it is different from this topic. Describe what you are trying to do and what the issues are.
-
@Jmrjj please how can I make a filter to a tree