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. QTableview Search

QTableview Search

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.4k Views 2 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.
  • A Offline
    A Offline
    Adan895
    wrote on last edited by Adan895
    #1

    Hello everyone, i am doing a search engine for a QTableview, and this is what i have until now:

    void Inventory::on_Search_clicked(){
    
        QModelIndexList matchList = ui->tableView->model()->match(ui->tableView->model()->index(0,0), Qt::EditRole, ui->lineEdit->text(), -1,  Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap));
    
        if(matchList.count()>=1){
         
                ui->tableView->setCurrentIndex(matchList.first());
                ui->tableView->scrollTo(matchList.first()); 
                }
        
        QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav");
    }
    

    It works as required, but i want to add another functionality:

    This code search for specific matches with a QLineedit and it selects and scrolls to the first value of the QModelindexList, but, if i click a second time searching the same text on the QLineedit, i would like the selection to scroll to the 2 item on the QModelindexList, i have tried many things with no luck, i hope you can help on this, Thanks!

    1 Reply Last reply
    0
    • A Offline
      A Offline
      Adan895
      wrote on last edited by Adan895
      #7

      Well, at the end i figured it out:

      void Registry::on_Search_clicked(){
      
          from++;
          QString text = ui->lineEdit_2->text();
          if(text.isEmpty())
                      return;
                  QModelIndexList indexes = ui->tableView->model()->match(ui->tableView->model()->index(0,0),Qt::EditRole,text,-1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap));
      
                  if(indexes.length() > from){
                      QModelIndex ix = indexes.at(from);
                      ui->tableView->setCurrentIndex(ix);
                      ui->tableView->scrollTo(ix);
                  }
      
                  QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav");
      }
      

      The int variable "from" is declared as public on the header.

      Thanks!

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Keep the current search as well as the index of the item in it that you are showing. Then have a next button that will increment that index and set the focus on the new "current" index.

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

        A 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Keep the current search as well as the index of the item in it that you are showing. Then have a next button that will increment that index and set the focus on the new "current" index.

          A Offline
          A Offline
          Adan895
          wrote on last edited by
          #3

          @SGaist i was thinkng about it, but, is there a way to do it using the just the Search button? jeje

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Yes, do the same and add the logic to reset the state once you change the text searched.

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

            A 1 Reply Last reply
            0
            • SGaistS SGaist

              Yes, do the same and add the logic to reset the state once you change the text searched.

              A Offline
              A Offline
              Adan895
              wrote on last edited by
              #5

              @SGaist I cannot do the logic of it, its driving me crazy xd

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                What part are you failing on ?

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

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Adan895
                  wrote on last edited by Adan895
                  #7

                  Well, at the end i figured it out:

                  void Registry::on_Search_clicked(){
                  
                      from++;
                      QString text = ui->lineEdit_2->text();
                      if(text.isEmpty())
                                  return;
                              QModelIndexList indexes = ui->tableView->model()->match(ui->tableView->model()->index(0,0),Qt::EditRole,text,-1, Qt::MatchFlags(Qt::MatchContains|Qt::MatchWrap));
                  
                              if(indexes.length() > from){
                                  QModelIndex ix = indexes.at(from);
                                  ui->tableView->setCurrentIndex(ix);
                                  ui->tableView->scrollTo(ix);
                              }
                  
                              QSound::play("/home/adan/Groostore/Resources/Sounds/Update.wav");
                  }
                  

                  The int variable "from" is declared as public on the header.

                  Thanks!

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    There's no need to declare it as public, it's an internal implementation detail that should not be exposed.

                    Note that you don't seem to reset from anywhere so you will likely have strange results. Like for example always starting to show the second found entry.

                    Another issue here is that you re-do the search every time so if you have a big table you are killing performances. You might want to take some time to re-think the search functionality.

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

                    A 1 Reply Last reply
                    2
                    • SGaistS SGaist

                      There's no need to declare it as public, it's an internal implementation detail that should not be exposed.

                      Note that you don't seem to reset from anywhere so you will likely have strange results. Like for example always starting to show the second found entry.

                      Another issue here is that you re-do the search every time so if you have a big table you are killing performances. You might want to take some time to re-think the search functionality.

                      A Offline
                      A Offline
                      Adan895
                      wrote on last edited by
                      #9

                      @SGaist Hello, the from variable is being reseted on a QlineEdit slot called textChanged; where can i declare the from variable to avoid making it public? because everytime the Search button event is closed the variable int is reseted, and i need to keep it until textChanged is called. Thanks

                      mrjjM 1 Reply Last reply
                      0
                      • A Adan895

                        @SGaist Hello, the from variable is being reseted on a QlineEdit slot called textChanged; where can i declare the from variable to avoid making it public? because everytime the Search button event is closed the variable int is reseted, and i need to keep it until textChanged is called. Thanks

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

                        @Adan895
                        Hi
                        The from++; variable should be a member of the class. Just placed in the
                        private section and not public since its used for something internal and
                        the outside world should not have access to it. Only the class.

                        1 Reply Last reply
                        1

                        • Login

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