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. QTreeWidget with LineEdit to filter and search element in my tree
QtWS25 Last Chance

QTreeWidget with LineEdit to filter and search element in my tree

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 5 Posters 4.1k Views
  • 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.
  • C Offline
    C Offline
    coilo
    wrote on 3 Aug 2021, 14:55 last edited by coilo
    #1

    Hello guys,

    I am new to the world of Qt. I am trying to do a filter for my QTreeWidget. I have a list of items in my tree and I would like to use a search input (with a LineEdit) to find my item from my list.
    Here the pic of my GUI so that you can better understand :

    So when I write a letter in my LineEdit i would like to show only items who started with this letter.
    But unfortunately i have no idea how to deal with it.

    If someone can help me, thank you very much.

    C 1 Reply Last reply 3 Aug 2021, 15:10
    0
    • C coilo
      3 Aug 2021, 14:55

      Hello guys,

      I am new to the world of Qt. I am trying to do a filter for my QTreeWidget. I have a list of items in my tree and I would like to use a search input (with a LineEdit) to find my item from my list.
      Here the pic of my GUI so that you can better understand :

      So when I write a letter in my LineEdit i would like to show only items who started with this letter.
      But unfortunately i have no idea how to deal with it.

      If someone can help me, thank you very much.

      C Offline
      C Offline
      CP71
      wrote on 3 Aug 2021, 15:10 last edited by CP71 8 Mar 2021, 15:11
      #2

      @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

      C 1 Reply Last reply 3 Aug 2021, 15:20
      1
      • V Offline
        V Offline
        VRonin
        wrote on 3 Aug 2021, 15:12 last edited by
        #3

        Instead of using QTreeWidget split the model and the view (QStandardItemModel+QTreeView)
        Then you can just use a QSortFilterProxyModel in-between to do the filtering

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        C 1 Reply Last reply 3 Aug 2021, 15:23
        4
        • C CP71
          3 Aug 2021, 15:10

          @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

          C Offline
          C Offline
          coilo
          wrote on 3 Aug 2021, 15:20 last edited by
          #4

          @CP71
          Hmmm i am going to try your solution ! Thanks for the help

          C 1 Reply Last reply 3 Aug 2021, 15:24
          1
          • V VRonin
            3 Aug 2021, 15:12

            Instead of using QTreeWidget split the model and the view (QStandardItemModel+QTreeView)
            Then you can just use a QSortFilterProxyModel in-between to do the filtering

            C Offline
            C Offline
            coilo
            wrote on 3 Aug 2021, 15:23 last edited by
            #5

            @VRonin
            The problem is that I have already used this tree for other functions. So I would like to avoid redoing everything and keep the QTreeWidget format. But yeah you're right, I don't think that the QtreeWidget format is the most appropriate :(

            1 Reply Last reply
            0
            • C coilo
              3 Aug 2021, 15:20

              @CP71
              Hmmm i am going to try your solution ! Thanks for the help

              C Offline
              C Offline
              CP71
              wrote on 3 Aug 2021, 15:24 last edited by
              #6

              @coilo
              You are welcome.
              Please, write the result of your test, so I know if it is a good way for future
              Thanks

              C 1 Reply Last reply 3 Aug 2021, 15:39
              1
              • C CP71
                3 Aug 2021, 15:24

                @coilo
                You are welcome.
                Please, write the result of your test, so I know if it is a good way for future
                Thanks

                C Offline
                C Offline
                coilo
                wrote on 3 Aug 2021, 15:39 last edited by
                #7

                @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 ! =)

                C 1 Reply Last reply 3 Aug 2021, 15:48
                0
                • C coilo
                  3 Aug 2021, 15:39

                  @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 ! =)

                  C Offline
                  C Offline
                  CP71
                  wrote on 3 Aug 2021, 15:48 last edited by
                  #8

                  @coilo
                  try
                  https://doc.qt.io/qt-5/qtreewidgetitem.html#text

                  1 Reply Last reply
                  1
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 3 Aug 2021, 18:09 last edited by
                    #9

                    Hi,

                    For the searching part, no need to complicate stuff, QTreeWidget has you covered with findItems.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    C 1 Reply Last reply 3 Aug 2021, 18:22
                    1
                    • SGaistS SGaist
                      3 Aug 2021, 18:09

                      Hi,

                      For the searching part, no need to complicate stuff, QTreeWidget has you covered with findItems.

                      C Offline
                      C Offline
                      CP71
                      wrote on 3 Aug 2021, 18:22 last edited by CP71 8 Mar 2021, 18:28
                      #10

                      @coilo
                      as @SGaist says

                      https://stackoverflow.com/questions/29694416/qtreewidgetitem-find-child-by-text

                      I'm sure that my first idea is not a good way! :(

                      1 Reply Last reply
                      1
                      • C Offline
                        C Offline
                        coilo
                        wrote on 4 Aug 2021, 08:47 last edited by coilo
                        #11
                        This post is deleted!
                        JonBJ 1 Reply Last reply 4 Aug 2021, 09:01
                        0
                        • C coilo
                          4 Aug 2021, 08:47

                          This post is deleted!

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on 4 Aug 2021, 09:01 last edited by
                          #12

                          @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 QTreeWidget it 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 own QTreeView plus a QSortFilterProxyModel, 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 QTreeWidget you will need to clear its current members and then re-add the desired new items from sublist into your ui->treeWidget, not just call ui->treeWidget->setCurrentItem() (which only selects an existing item already in a QTreeWidget).

                          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.

                          1 Reply Last reply
                          2
                          • V Offline
                            V Offline
                            VRonin
                            wrote on 4 Aug 2021, 09:40 last edited by VRonin 8 Apr 2021, 09:41
                            #13

                            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 QTreeView right next to your QTreeWidget.
                            • Add a private QSortFilterProxyModel* m_proxy; member to MainWindow
                            • in the MainWindow constructor add:
                            m_proxy  = new QSortFilterProxyModel(this);
                            m_proxy->setSourceModel(ui->treeWidget->model());
                            ui->treeView->setModel(m_proxy);
                            ui->treeWidget->hide();
                            
                            • on_lineEdit_textChanged becomes:
                            void MainWindow::on_lineEdit_textChanged(const QString &arg1)
                            {
                                m_proxy->setFilterRegularExpression(arg1);
                            }
                            

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            1 Reply Last reply
                            4
                            • C Offline
                              C Offline
                              coilo
                              wrote on 4 Aug 2021, 14:18 last edited by
                              #14

                              Hmmm ok i tried what you suggested me and it works yeah ! But i will consider changing my QTreeWidget into QStandardItemModel+QSortFilterProxyModel+QTreeView to do it properly in the future.

                              Thanks a lot guys !

                              1 Reply Last reply
                              0

                              1/14

                              3 Aug 2021, 14:55

                              • Login

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