Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Search and select item in QTreeWidget based on Search String

Search and select item in QTreeWidget based on Search String

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 6.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM mrjj

    Hi
    So it colored all items ???

    M Offline
    M Offline
    MostafaEzzat
    wrote on last edited by MostafaEzzat
    #9

    @mrjj

    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

    mrjjM 1 Reply Last reply
    0
    • M MostafaEzzat

      @mrjj

      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

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #10

      @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.

      M 1 Reply Last reply
      0
      • mrjjM mrjj

        @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.

        M Offline
        M Offline
        MostafaEzzat
        wrote on last edited by
        #11

        @mrjj

        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) ;
          
        mrjjM 1 Reply Last reply
        0
        • M MostafaEzzat

          @mrjj

          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) ;
            
          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #12

          @MostafaEzzat
          Hi
          Ehh that is nothing like I expected. o.O

          You 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
          alt text

          Test Project
          https://www.dropbox.com/s/zu5tc2hobdwt56i/justDefualt.zip?dl=0

          Do 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.

          M 1 Reply Last reply
          2
          • mrjjM mrjj

            @MostafaEzzat
            Hi
            Ehh that is nothing like I expected. o.O

            You 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
            alt text

            Test Project
            https://www.dropbox.com/s/zu5tc2hobdwt56i/justDefualt.zip?dl=0

            Do 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.

            M Offline
            M Offline
            MostafaEzzat
            wrote on last edited by
            #13
            This post is deleted!
            M 1 Reply Last reply
            0
            • M MostafaEzzat

              This post is deleted!

              M Offline
              M Offline
              MostafaEzzat
              wrote on last edited by
              #14
              This post is deleted!
              1 Reply Last reply
              0
              • M Offline
                M Offline
                MostafaEzzat
                wrote on last edited by
                #15

                @mrjj

                Thanks i got it after a while , it works very well

                Appreciated

                mrjjM 1 Reply Last reply
                1
                • M MostafaEzzat

                  @mrjj

                  Thanks i got it after a while , it works very well

                  Appreciated

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #16

                  @MostafaEzzat
                  Good to hear :)

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    John H. BROOKS
                    wrote on last edited by
                    #17

                    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)

                    JonBJ 1 Reply Last reply
                    0
                    • J John H. BROOKS

                      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)

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #18

                      @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.

                      O 1 Reply Last reply
                      1
                      • JonBJ JonB

                        @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.

                        O Offline
                        O Offline
                        OdinUci
                        wrote on last edited by
                        #19

                        @Jmrjj please how can I make a filter to a tree

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved