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. Searching bar in tree widget
QtWS25 Last Chance

Searching bar in tree widget

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5tree widgetsearch
19 Posts 5 Posters 1.7k 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.
  • D deleted286
    29 Mar 2021, 11:00

    I have the code below for searching items in tree widget. I doesnt search. When i finish editing, i cant see any item on widget.
    It doesnt have any error. But when i debug, i saw that "arama" is empty

    void MainWindow::on_ara_editingFinished()
    {
    
        QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains, 0);
        for(QTreeWidgetItem* item  : arama)
        {
            qDebug() << ui->ara->text();
            ui->treeWidget2->setCurrentItem(item);
        }
    }
    
    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 29 Mar 2021, 11:55 last edited by
    #2

    @suslucoder What does ui->ara->text() return? In which column do you want to search? Is there at least one item in the tree widget in the column you're searching containing the text ui->ara->text() returns?

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    D 1 Reply Last reply 29 Mar 2021, 12:19
    1
    • J jsulm
      29 Mar 2021, 11:55

      @suslucoder What does ui->ara->text() return? In which column do you want to search? Is there at least one item in the tree widget in the column you're searching containing the text ui->ara->text() returns?

      D Offline
      D Offline
      deleted286
      wrote on 29 Mar 2021, 12:19 last edited by
      #3

      @jsulm
      I've changed my code to

         QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains|Qt::MatchRecursive, 0);
          for(QTreeWidgetItem* item  : arama)
          {
             ui->treeWidget2->setCurrentItem(item, 0);
            // item->setBackground(0, Qt::red);
          }
      

      but setCurrentItem is set one item..of course...

      But i want to see every item that contains the given text

      J 1 Reply Last reply 29 Mar 2021, 12:39
      0
      • D deleted286
        29 Mar 2021, 12:19

        @jsulm
        I've changed my code to

           QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains|Qt::MatchRecursive, 0);
            for(QTreeWidgetItem* item  : arama)
            {
               ui->treeWidget2->setCurrentItem(item, 0);
              // item->setBackground(0, Qt::red);
            }
        

        but setCurrentItem is set one item..of course...

        But i want to see every item that contains the given text

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 29 Mar 2021, 12:39 last edited by
        #4

        @suslucoder said in Searching bar in tree widget:

        But i want to see every item that contains the given text

        https://doc.qt.io/qt-5/qtreewidgetitem.html#setSelected

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        D 1 Reply Last reply 29 Mar 2021, 12:44
        2
        • J jsulm
          29 Mar 2021, 12:39

          @suslucoder said in Searching bar in tree widget:

          But i want to see every item that contains the given text

          https://doc.qt.io/qt-5/qtreewidgetitem.html#setSelected

          D Offline
          D Offline
          deleted286
          wrote on 29 Mar 2021, 12:44 last edited by
          #5

          @jsulm you suggest me to use setSelected ?

          J 1 Reply Last reply 29 Mar 2021, 12:45
          0
          • D deleted286
            29 Mar 2021, 12:44

            @jsulm you suggest me to use setSelected ?

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 29 Mar 2021, 12:45 last edited by
            #6

            @suslucoder Yes, I do - you want to select items, right? The method I suggested does exactly that...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            D 1 Reply Last reply 29 Mar 2021, 12:47
            0
            • J jsulm
              29 Mar 2021, 12:45

              @suslucoder Yes, I do - you want to select items, right? The method I suggested does exactly that...

              D Offline
              D Offline
              deleted286
              wrote on 29 Mar 2021, 12:47 last edited by
              #7

              @jsulm No, i dont want to select :D
              I have a search bar, it doesnt set the contains this is my problem

              J D 2 Replies Last reply 29 Mar 2021, 12:50
              0
              • D deleted286
                29 Mar 2021, 12:47

                @jsulm No, i dont want to select :D
                I have a search bar, it doesnt set the contains this is my problem

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 29 Mar 2021, 12:50 last edited by
                #8

                @suslucoder said in Searching bar in tree widget:

                I have a search bar, it doesnt set the contains this is my problem

                Then can you please explain what you want to do with items you found? Because I don't understand...

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • D deleted286
                  29 Mar 2021, 12:47

                  @jsulm No, i dont want to select :D
                  I have a search bar, it doesnt set the contains this is my problem

                  D Offline
                  D Offline
                  deleted286
                  wrote on 29 Mar 2021, 12:52 last edited by deleted286
                  #9

                  @suslucoder

                  QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains|Qt::MatchRecursive, 0);
                      for(QTreeWidgetItem* item  : arama)
                      {
                        ui->treeWidget2->setCurrentItem(item, 0);  
                       // item->setBackground(0, Qt::cyan);
                  
                      }
                  

                  when i comment item->setBackground(0, Qt::cyan);, unless the tree has more than 1 item that at give via ara->text, it show the exactly 1 item.

                  When i un comment the

                  item->setBackground(0, Qt::cyan);
                  

                  the code finds the every item that contains the text, but the text setCurrent item has is blue, the other one is cyan.

                  When i only wrote

                  item->setBackground(0, Qt::cyan);
                  

                  search doesnt work

                  J 1 Reply Last reply 29 Mar 2021, 12:55
                  0
                  • D deleted286
                    29 Mar 2021, 12:52

                    @suslucoder

                    QList<QTreeWidgetItem*> arama = ui->treeWidget2->findItems(ui->ara->text(), Qt::MatchContains|Qt::MatchRecursive, 0);
                        for(QTreeWidgetItem* item  : arama)
                        {
                          ui->treeWidget2->setCurrentItem(item, 0);  
                         // item->setBackground(0, Qt::cyan);
                    
                        }
                    

                    when i comment item->setBackground(0, Qt::cyan);, unless the tree has more than 1 item that at give via ara->text, it show the exactly 1 item.

                    When i un comment the

                    item->setBackground(0, Qt::cyan);
                    

                    the code finds the every item that contains the text, but the text setCurrent item has is blue, the other one is cyan.

                    When i only wrote

                    item->setBackground(0, Qt::cyan);
                    

                    search doesnt work

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 29 Mar 2021, 12:55 last edited by
                    #10

                    @suslucoder Why do you call setCurrentItem?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    D 1 Reply Last reply 29 Mar 2021, 12:57
                    0
                    • J jsulm
                      29 Mar 2021, 12:55

                      @suslucoder Why do you call setCurrentItem?

                      D Offline
                      D Offline
                      deleted286
                      wrote on 29 Mar 2021, 12:57 last edited by
                      #11

                      @jsulm yes i dont i know why im using it.
                      I saw in somewehere.
                      But when i delete it, it doesnt work

                      J 1 Reply Last reply 29 Mar 2021, 13:01
                      0
                      • D deleted286
                        29 Mar 2021, 12:57

                        @jsulm yes i dont i know why im using it.
                        I saw in somewehere.
                        But when i delete it, it doesnt work

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 29 Mar 2021, 13:01 last edited by
                        #12

                        @suslucoder said in Searching bar in tree widget:

                        But when i delete it, it doesnt work

                        In what way it doesn't work? setBackground should work without setCurrentItem, they are unrelated.

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        D 1 Reply Last reply 29 Mar 2021, 13:05
                        0
                        • J jsulm
                          29 Mar 2021, 13:01

                          @suslucoder said in Searching bar in tree widget:

                          But when i delete it, it doesnt work

                          In what way it doesn't work? setBackground should work without setCurrentItem, they are unrelated.

                          D Offline
                          D Offline
                          deleted286
                          wrote on 29 Mar 2021, 13:05 last edited by
                          #13

                          @jsulm when i delete

                          ui->treeWidget2->setCurrentItem(item, 0);
                          

                          the hole code doesnt work. without any error messages

                          D 1 Reply Last reply 29 Mar 2021, 13:15
                          0
                          • D deleted286
                            29 Mar 2021, 13:05

                            @jsulm when i delete

                            ui->treeWidget2->setCurrentItem(item, 0);
                            

                            the hole code doesnt work. without any error messages

                            D Offline
                            D Offline
                            deleted286
                            wrote on 29 Mar 2021, 13:15 last edited by
                            #14

                            @suslucoder I debug and, it finds the items but doesnt show me on tree widget

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 29 Mar 2021, 18:34 last edited by
                              #15

                              Hi,

                              Why don't you just modify the background of each item of the list and nothing else ?

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

                              D 1 Reply Last reply 30 Mar 2021, 10:28
                              0
                              • N Offline
                                N Offline
                                nagesh
                                wrote on 29 Mar 2021, 23:54 last edited by
                                #16

                                @suslucoder said in Searching bar in tree widget:

                                When i only wrote
                                item->setBackground(0, Qt::cyan);

                                search doesnt work

                                I think arguement for setBackground is QBrush not the color
                                void QTreeWidgetItem::setBackground(int column, const QBrush &brush)

                                1 Reply Last reply
                                0
                                • C Online
                                  C Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 30 Mar 2021, 04:57 last edited by
                                  #17

                                  @nagesh said in Searching bar in tree widget:

                                  I think arguement for setBackground is QBrush not the color

                                  Can you please elaborate this?
                                  https://doc.qt.io/qt-5/qbrush.html#QBrush-3

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  1 Reply Last reply
                                  1
                                  • S SGaist
                                    29 Mar 2021, 18:34

                                    Hi,

                                    Why don't you just modify the background of each item of the list and nothing else ?

                                    D Offline
                                    D Offline
                                    deleted286
                                    wrote on 30 Mar 2021, 10:28 last edited by
                                    #18

                                    @SGaist when i modify the background, for highligting the searching items,
                                    even the text was changed, it keep the oldest item.

                                    I search for a and highligh its background.
                                    Then i delete a, search for b

                                    It shows me the both a and b

                                    1 Reply Last reply
                                    0
                                    • N Offline
                                      N Offline
                                      nagesh
                                      wrote on 30 Mar 2021, 12:00 last edited by
                                      #19

                                      @Christian-Ehrlicher thanks for the correction.

                                      @suslucoder it's showing old search items with different color because you have set it's background color in last search..
                                      It's better to set all with default color and matched items in highlight colour for each search

                                      1 Reply Last reply
                                      2

                                      11/19

                                      29 Mar 2021, 12:57

                                      • Login

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