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. Finding data(QString) of item in QAbstactTableModel
QtWS25 Last Chance

Finding data(QString) of item in QAbstactTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 3 Posters 806 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.
  • K Offline
    K Offline
    keyboard_hellchang
    wrote on last edited by
    #1

    Hi!
    My qt version is 5.14.2.
    The environment is CentOS7.

    Now I'm trying to implement the ability to find data of an item in QTableView through QLineEdit.

    But it is not easy to solve this problem.

    Here's an example you've tried.

    • proxy (QSortFilterProxyModel) - > mapToSource
    • Match function (QAbstractItemModel) -> Finds only one column.
    • FindItems function (QStandardItemModel) -> The row (), column () values of QModelIndex are printed -1.

    I tried the above but could not solve it.
    I want to find it on the whole table.

    Help me

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Please show some code on what you you did. You can search your model by iterating over data() and then use the found index to select it in the view.

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

      K 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Please show some code on what you you did. You can search your model by iterating over data() and then use the found index to select it in the view.

        K Offline
        K Offline
        keyboard_hellchang
        wrote on last edited by keyboard_hellchang
        #3

        @Christian-Ehrlicher
        I have implemented it as below as you told me.
        But the number of data is now 100,000.
        The rate of finding data is extremely slow.

        mainwindow.cpp

        - QLineEdit slot function
         
        void MainWindow::slot_tableview_search()
        {                                          
            QString str = ui->x_search_ledit->text().simplified();
                                                   
            for(int i = 0; i < ui->tableview->model()->rowCount(); i++)
            {                                      
                for(int j = 0; j < ui->tableview->model()->columnCount(); j++) 
                {                                  
                    if(str == m_tableview_model->data(ui->tableview->model()->index(i, j), Qt::DisplayRole))
                    {
                        ui->tableview->setCurrentIndex(ui->tableview->model()->index(i, j));
                        ui->tableview->scrollTo(ui->tableview->model()->index(i, j));
                    }                              
                    else                           
                    {
                       ;
                    }                              
                }                                  
            }                                      
        }
        
        model.cpp
        - data() function
        QVariant Model::data(const QModelIndex &index, int role) const                              
        {                                                                                                    
            if( role==Qt::DisplayRole )                                                                      
            {                                                                                                
                if((index.row() == rowHeaderList.size() - 1) && (rowList[index.row()][index.column()] == 0)) 
                {                                                                                            
                    return QString();                                                                        
                }                                                                                            
                else                                                                                         
                {                                                                                            
                    return QString::number(rowList[index.row()][index.column()], 16);                        
                }                                                                                            
            }                                                                                                
                                                                                                             
            return QVariant::Invalid;                                                                        
        }
        
        JonBJ 1 Reply Last reply
        0
        • Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Please use the code - tags so your code is readable.

          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
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            And do you find your data?
            Why do you create the index three times?

            if(str == m_tableview_model->data(ui->tableview->model()->index(i, j), Qt::DisplayRole))
            {
            ui->tableview->setCurrentIndex(ui->tableview->model()->index(i, j));
            ui->tableview->scrollTo(ui->tableview->model()->index(i, j));

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

            K 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              And do you find your data?
              Why do you create the index three times?

              if(str == m_tableview_model->data(ui->tableview->model()->index(i, j), Qt::DisplayRole))
              {
              ui->tableview->setCurrentIndex(ui->tableview->model()->index(i, j));
              ui->tableview->scrollTo(ui->tableview->model()->index(i, j));

              K Offline
              K Offline
              keyboard_hellchang
              wrote on last edited by
              #6

              @Christian-Ehrlicher

              I'm sorry. I've never done a code upload before, so I'm not good at it.

              I want to find matching data in the entire table.
              The search speed is slowed down when applying the above code.

              The reason why I use indexes three times

              1. Use of an if condition
              2. Scroll
              3. Data focus
              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Then create the index once and use it three times.
                But you still did not answer my question - do you find your string you're looking for with this loop above?

                And again: please modify your first post to use code tags.

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

                K 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  Then create the index once and use it three times.
                  But you still did not answer my question - do you find your string you're looking for with this loop above?

                  And again: please modify your first post to use code tags.

                  K Offline
                  K Offline
                  keyboard_hellchang
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher

                  Code tag is completed.

                  I'm trying to find a string entered in QLineEdit on the whole table.
                  so I ran the for loop to search the whole table.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Online
                    Christian EhrlicherC Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @keyboard_hellchang said in Finding data(QString) of item in QAbstactTableModel:

                    so I ran the for loop to search the whole table.

                    All fine, but do you find it? If not make sure data() returns the strings you're expecting. Maybe add a debug output to see what you get.

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

                    K 1 Reply Last reply
                    0
                    • K keyboard_hellchang

                      @Christian-Ehrlicher
                      I have implemented it as below as you told me.
                      But the number of data is now 100,000.
                      The rate of finding data is extremely slow.

                      mainwindow.cpp

                      - QLineEdit slot function
                       
                      void MainWindow::slot_tableview_search()
                      {                                          
                          QString str = ui->x_search_ledit->text().simplified();
                                                                 
                          for(int i = 0; i < ui->tableview->model()->rowCount(); i++)
                          {                                      
                              for(int j = 0; j < ui->tableview->model()->columnCount(); j++) 
                              {                                  
                                  if(str == m_tableview_model->data(ui->tableview->model()->index(i, j), Qt::DisplayRole))
                                  {
                                      ui->tableview->setCurrentIndex(ui->tableview->model()->index(i, j));
                                      ui->tableview->scrollTo(ui->tableview->model()->index(i, j));
                                  }                              
                                  else                           
                                  {
                                     ;
                                  }                              
                              }                                  
                          }                                      
                      }
                      
                      model.cpp
                      - data() function
                      QVariant Model::data(const QModelIndex &index, int role) const                              
                      {                                                                                                    
                          if( role==Qt::DisplayRole )                                                                      
                          {                                                                                                
                              if((index.row() == rowHeaderList.size() - 1) && (rowList[index.row()][index.column()] == 0)) 
                              {                                                                                            
                                  return QString();                                                                        
                              }                                                                                            
                              else                                                                                         
                              {                                                                                            
                                  return QString::number(rowList[index.row()][index.column()], 16);                        
                              }                                                                                            
                          }                                                                                                
                                                                                                                           
                          return QVariant::Invalid;                                                                        
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @keyboard_hellchang said in Finding data(QString) of item in QAbstactTableModel:

                      But the number of data is now 100,000.
                      The rate of finding data is extremely slow.

                      • How slow is "slow"? If you have 100k+ items to search through sequentially it will take a bit of time.

                      • As @Christian-Ehrlicher has said, there is a place where you create the same index() three times, it might help if you only do that once, I don't know just how expensive that operation is.

                      • You convert every single element from a stored number to a string, to do the comparison. You might be better converting the string you are searching for to a number once, and then searching the elements for a number instead.

                      • You might directly search your rowList data structure instead of going via data().

                      • Finally, presumably you are not actually showing 100k+ items in a view, that would be too much for the user, so do you only need to search through whatever subset of the model you are showing in the view?

                      1 Reply Last reply
                      2
                      • Christian EhrlicherC Christian Ehrlicher

                        @keyboard_hellchang said in Finding data(QString) of item in QAbstactTableModel:

                        so I ran the for loop to search the whole table.

                        All fine, but do you find it? If not make sure data() returns the strings you're expecting. Maybe add a debug output to see what you get.

                        K Offline
                        K Offline
                        keyboard_hellchang
                        wrote on last edited by
                        #11

                        @Christian-Ehrlicher
                        I've almost solved this problem.
                        The feature is perfect except for the search speed.

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Online
                          Christian EhrlicherC Online
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          So what's your current solution? Did you implemented all what @JonB suggested? Esp. the conversions and the direct search in the model will help for sure.

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

                          K 1 Reply Last reply
                          1
                          • Christian EhrlicherC Christian Ehrlicher

                            So what's your current solution? Did you implemented all what @JonB suggested? Esp. the conversions and the direct search in the model will help for sure.

                            K Offline
                            K Offline
                            keyboard_hellchang
                            wrote on last edited by
                            #13

                            @Christian-Ehrlicher
                            Yes, we have proceeded with all the proposals.
                            Also, this program is being used too well after completion.
                            Thank you!!

                            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