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. How to accelerate the selection in the QTableView ?
Forum Updated to NodeBB v4.3 + New Features

How to accelerate the selection in the QTableView ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 593 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.
  • cyberpunkerC Offline
    cyberpunkerC Offline
    cyberpunker
    wrote on last edited by cyberpunker
    #1

    I use the MVC to show a bund of of filenames , and there are opertions of selecting all and dissecting all in the CTableView . The number of files may thousands , so , I write a thread to do these ,like:

    for (int row = 0; row < m_ImgMgr->m_TableModel->rowCount(); row++)
    	{
    
    
    		//QtSleep(5);
    
    		for (int column = 0; column < m_ImgMgr->m_TableModel->columnCount(); column++)
    		{
    
    			//LogLog( QString(" sel 1") ) ;
    			m_ImgMgr->listImages2->selectionModel()->select(m_ImgMgr->listImages2->model()->index(row, column), QItemSelectionModel::Select);
    
    		}
    
    		//LogLog(QString(" sel 2"));
    
    		m_ImgMgr->labelImageIndex->setText(QString::number(row+1) + QString("/") + QString::number(m_ImgMgr->m_TableModel->rowCount()));
    		
    		//LogLog(QString(" sel 3"));
    
    	}
    

    but consuming time is not acceptable . And if the number is very large , the operation seems become more and more slowly by the end of the loop , it can be seen by the count number displayed in the GUI .

    I also tried like below:

    	QItemSelection sel = m_ImgMgr->listImages2->selectionModel()->selection( ) ;
    
    	QModelIndex topleft     = m_ImgMgr->listImages2->model()->index( 0 , 0 ) ;
    	QModelIndex bottonright = m_ImgMgr->listImages2->model()->index(m_ImgMgr->m_TableModel->rowCount(), 3 ) ;
    
    	sel.select( topleft  , bottonright ) ;
    

    no funtional.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      I can't reproduce the problem. try this minimal example and see if it works for you:

      #include <QTableView>
      #include <QVBoxLayout>
      #include <QStandardItemModel>
      #include <QApplication>
      #include <QPushButton>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QWidget mainWid;
          QAbstractItemModel* model = new QStandardItemModel(&mainWid);
          model->insertColumns(0,4);
          model->insertRows(0,5000);
          for(int rowIter = 0, maxRow = model->rowCount();rowIter<maxRow;++rowIter){
              for(int colIter = 0, maxCol = model->columnCount();colIter<maxCol;++colIter){
                  model->setData(model->index(rowIter,colIter),QStringLiteral("%1,%2").arg(rowIter+1).arg(colIter+1));
              }
          }
          QTableView* table = new QTableView(&mainWid);
          table->setModel(model);
          QPushButton* selectAllButton = new QPushButton(QStringLiteral("Select All"),&mainWid);
          QObject::connect(selectAllButton,&QPushButton::clicked,table,[table]()->void{
              const QItemSelection selec(table->model()->index(0,0),table->model()->index(table->model()->rowCount()-1,table->model()->columnCount()-1));
              table->selectionModel()->select(selec,QItemSelectionModel::ClearAndSelect);
          });
          QVBoxLayout* mainLay = new QVBoxLayout(&mainWid);
          mainLay->addWidget(selectAllButton);
          mainLay->addWidget(table);
          mainWid.show();
          return a.exec();
      }
      

      P.S.

      I write a thread to do these

      Don't do GUI operations in secondary threads

      "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

      cyberpunkerC 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        How should your second approach work when you don't pass it to the selection model?
        And doing stuff for a model/view in a thread will not work out of the box since the models and views are not threadsafe

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

        cyberpunkerC 1 Reply Last reply
        2
        • VRoninV VRonin

          I can't reproduce the problem. try this minimal example and see if it works for you:

          #include <QTableView>
          #include <QVBoxLayout>
          #include <QStandardItemModel>
          #include <QApplication>
          #include <QPushButton>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              QWidget mainWid;
              QAbstractItemModel* model = new QStandardItemModel(&mainWid);
              model->insertColumns(0,4);
              model->insertRows(0,5000);
              for(int rowIter = 0, maxRow = model->rowCount();rowIter<maxRow;++rowIter){
                  for(int colIter = 0, maxCol = model->columnCount();colIter<maxCol;++colIter){
                      model->setData(model->index(rowIter,colIter),QStringLiteral("%1,%2").arg(rowIter+1).arg(colIter+1));
                  }
              }
              QTableView* table = new QTableView(&mainWid);
              table->setModel(model);
              QPushButton* selectAllButton = new QPushButton(QStringLiteral("Select All"),&mainWid);
              QObject::connect(selectAllButton,&QPushButton::clicked,table,[table]()->void{
                  const QItemSelection selec(table->model()->index(0,0),table->model()->index(table->model()->rowCount()-1,table->model()->columnCount()-1));
                  table->selectionModel()->select(selec,QItemSelectionModel::ClearAndSelect);
              });
              QVBoxLayout* mainLay = new QVBoxLayout(&mainWid);
              mainLay->addWidget(selectAllButton);
              mainLay->addWidget(table);
              mainWid.show();
              return a.exec();
          }
          

          P.S.

          I write a thread to do these

          Don't do GUI operations in secondary threads

          cyberpunkerC Offline
          cyberpunkerC Offline
          cyberpunker
          wrote on last edited by
          #4

          @VRonin said in How to accelerate the selection in the QTableView ?:

          QTableView* table = new QTableView(&mainWid);

          So great , I am still totally ignorant to MVC .

          Really apreciation.

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            How should your second approach work when you don't pass it to the selection model?
            And doing stuff for a model/view in a thread will not work out of the box since the models and views are not threadsafe

            cyberpunkerC Offline
            cyberpunkerC Offline
            cyberpunker
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            thanks for your suggestion.

            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