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. Performance problem about QItemSelectionModel
Forum Updated to NodeBB v4.3 + New Features

Performance problem about QItemSelectionModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.0k 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.
  • N Offline
    N Offline
    NextSaturday
    wrote on last edited by
    #1

    I have a QTableView which has thousands of rows. When I use QItemSelectionModel to select multiple rows at the same time, my application will become very lag until the selection status of the QTableView is cleared.
    However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.
    The initialization of the QTableView is shown as follows:

    m_tableView = new QTableView(this);
    m_tableModel = new QStandardItemModel;
    
    m_tableView->setModel(m_tableModel);
    m_tableView->setWordWrap(false);
    m_tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    
    // fill m_tableView with data
    ......
    

    The following codes will select all the rows by QItemSelectionModel.

    m_tableView->clearSelection();
    
    const int rowCount = m_tableModel->rowCount();
    const int columnCount = m_tableModel->columnCount();
    QItemSelection selection;
    for (int i = 0; i < rowCount; ++i)
    {
    	QModelIndex left = m_tableModel->index(i, 0);
    	QModelIndex right = m_tableModel->index(i, columnCount - 1);
    	selection.merge(QItemSelection(left, right), QItemSelectionModel::Select);
    }
    
    QItemSelectionModel * sm = m_tableView->selectionModel();
    sm->select(selection, QItemSelectionModel::Select);
    

    Any suggestions about the reason for the problem? Or is there any better way to select multiple rows?

    JonBJ 1 Reply Last reply
    0
    • N NextSaturday

      I have a QTableView which has thousands of rows. When I use QItemSelectionModel to select multiple rows at the same time, my application will become very lag until the selection status of the QTableView is cleared.
      However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.
      The initialization of the QTableView is shown as follows:

      m_tableView = new QTableView(this);
      m_tableModel = new QStandardItemModel;
      
      m_tableView->setModel(m_tableModel);
      m_tableView->setWordWrap(false);
      m_tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
      m_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
      
      // fill m_tableView with data
      ......
      

      The following codes will select all the rows by QItemSelectionModel.

      m_tableView->clearSelection();
      
      const int rowCount = m_tableModel->rowCount();
      const int columnCount = m_tableModel->columnCount();
      QItemSelection selection;
      for (int i = 0; i < rowCount; ++i)
      {
      	QModelIndex left = m_tableModel->index(i, 0);
      	QModelIndex right = m_tableModel->index(i, columnCount - 1);
      	selection.merge(QItemSelection(left, right), QItemSelectionModel::Select);
      }
      
      QItemSelectionModel * sm = m_tableView->selectionModel();
      sm->select(selection, QItemSelectionModel::Select);
      

      Any suggestions about the reason for the problem? Or is there any better way to select multiple rows?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @NextSaturday
      Instead of a loop selecting one row at a time and merging, what is wrong with:

      QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
      

      ? But whether that will make any difference to speed I don't know.

      However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.

      Use QAbstractItemView::selectAll(). I would have thought that is what Ctrl+A would do.

      N 1 Reply Last reply
      0
      • JonBJ JonB

        @NextSaturday
        Instead of a loop selecting one row at a time and merging, what is wrong with:

        QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
        

        ? But whether that will make any difference to speed I don't know.

        However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.

        Use QAbstractItemView::selectAll(). I would have thought that is what Ctrl+A would do.

        N Offline
        N Offline
        NextSaturday
        wrote on last edited by
        #3

        @JonB said in Performance problem about QItemSelectionModel:

        @NextSaturday
        Instead of a loop selecting one row at a time and merging, what is wrong with:

        QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
        

        ? But whether that will make any difference to speed I don't know.

        However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.

        Use QAbstractItemView::selectAll(). I would have thought that is what Ctrl+A would do.

        Thanks, the method you suggested works as well as Ctrl + A.
        But what I really need is not select all rows. The above mentioned code is shown just for an example of QItemSelectionModel. I need to select multiple rows at the same time selectively. Maybe just one row, or maybe all rows except the first row. In this case, I cannot figure out any methods except a loop selecting and merging.

        JonBJ N 2 Replies Last reply
        0
        • N NextSaturday

          @JonB said in Performance problem about QItemSelectionModel:

          @NextSaturday
          Instead of a loop selecting one row at a time and merging, what is wrong with:

          QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
          

          ? But whether that will make any difference to speed I don't know.

          However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.

          Use QAbstractItemView::selectAll(). I would have thought that is what Ctrl+A would do.

          Thanks, the method you suggested works as well as Ctrl + A.
          But what I really need is not select all rows. The above mentioned code is shown just for an example of QItemSelectionModel. I need to select multiple rows at the same time selectively. Maybe just one row, or maybe all rows except the first row. In this case, I cannot figure out any methods except a loop selecting and merging.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @NextSaturday said in Performance problem about QItemSelectionModel:

          Maybe just one row, or maybe all rows except the first row. In this case, I cannot figure out any methods except a loop selecting and merging.

          Those for example could be achieved without loop via

          QItemSelection(m_tableModel->index(someRow, 0), m_tableModel->index(someRow, columnCount - 1))
          QItemSelection(m_tableModel->index(1, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
          

          I am surprised any of this makes much difference speed-wise. If you think QItemSelection::merge() is the culprit try playing around with your code to prove or disprove.

          N 2 Replies Last reply
          0
          • JonBJ JonB

            @NextSaturday said in Performance problem about QItemSelectionModel:

            Maybe just one row, or maybe all rows except the first row. In this case, I cannot figure out any methods except a loop selecting and merging.

            Those for example could be achieved without loop via

            QItemSelection(m_tableModel->index(someRow, 0), m_tableModel->index(someRow, columnCount - 1))
            QItemSelection(m_tableModel->index(1, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
            

            I am surprised any of this makes much difference speed-wise. If you think QItemSelection::merge() is the culprit try playing around with your code to prove or disprove.

            N Offline
            N Offline
            NextSaturday
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • N NextSaturday

              @JonB said in Performance problem about QItemSelectionModel:

              @NextSaturday
              Instead of a loop selecting one row at a time and merging, what is wrong with:

              QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
              

              ? But whether that will make any difference to speed I don't know.

              However, if I use Ctrl + A to select all rows of the QTableView, the application's performance will not be affected at all.

              Use QAbstractItemView::selectAll(). I would have thought that is what Ctrl+A would do.

              Thanks, the method you suggested works as well as Ctrl + A.
              But what I really need is not select all rows. The above mentioned code is shown just for an example of QItemSelectionModel. I need to select multiple rows at the same time selectively. Maybe just one row, or maybe all rows except the first row. In this case, I cannot figure out any methods except a loop selecting and merging.

              N Offline
              N Offline
              NextSaturday
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • JonBJ JonB

                @NextSaturday said in Performance problem about QItemSelectionModel:

                Maybe just one row, or maybe all rows except the first row. In this case, I cannot figure out any methods except a loop selecting and merging.

                Those for example could be achieved without loop via

                QItemSelection(m_tableModel->index(someRow, 0), m_tableModel->index(someRow, columnCount - 1))
                QItemSelection(m_tableModel->index(1, 0), m_tableModel->index(rowCount - 1, columnCount - 1))
                

                I am surprised any of this makes much difference speed-wise. If you think QItemSelection::merge() is the culprit try playing around with your code to prove or disprove.

                N Offline
                N Offline
                NextSaturday
                wrote on last edited by
                #7

                @JonB said in Performance problem about QItemSelectionModel:

                I am surprised any of this makes much difference speed-wise. If you think QItemSelection::merge() is the culprit try playing around with your code to prove or disprove.

                Sorry, I don't seem to express myself clearly. When I said the method you suggested work as well as Ctrl + A, I still have to use QItemSelection::merge(). Using the code you suggested alone cannot realize the selection work. QitemSelection and QItemSelectionModel must be used at the same time.

                The following code shows the test. According to my test, it appears as if the number of times QItemSelection::merge() is called is the reason for the problem, not the count of "merged rows".

                const int rowCount = m_tableModel->rowCount();
                const int columnCount = m_tableModel->columnCount();
                QItemSelection selection;
                
                // this code will lead to lag
                //for (int i = 0; i < rowCount; ++i)
                //{
                //	QModelIndex left = m_tableModel->index(i, 0);
                //	QModelIndex right = m_tableModel->index(i, columnCount - 1);
                //	selection.merge(QItemSelection(left, right), QItemSelectionModel::Select);
                //}
                
                // this code works as well as Ctrl + A
                selection.merge(QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1)), QItemSelectionModel::Select);
                
                QItemSelectionModel * sm = m_tableView->selectionModel();
                sm->select(selection, QItemSelectionModel::Select);
                

                Besides, the QTableView has thousands of rows. The rows need to be selected could change at any time.

                JonBJ 1 Reply Last reply
                0
                • N NextSaturday

                  @JonB said in Performance problem about QItemSelectionModel:

                  I am surprised any of this makes much difference speed-wise. If you think QItemSelection::merge() is the culprit try playing around with your code to prove or disprove.

                  Sorry, I don't seem to express myself clearly. When I said the method you suggested work as well as Ctrl + A, I still have to use QItemSelection::merge(). Using the code you suggested alone cannot realize the selection work. QitemSelection and QItemSelectionModel must be used at the same time.

                  The following code shows the test. According to my test, it appears as if the number of times QItemSelection::merge() is called is the reason for the problem, not the count of "merged rows".

                  const int rowCount = m_tableModel->rowCount();
                  const int columnCount = m_tableModel->columnCount();
                  QItemSelection selection;
                  
                  // this code will lead to lag
                  //for (int i = 0; i < rowCount; ++i)
                  //{
                  //	QModelIndex left = m_tableModel->index(i, 0);
                  //	QModelIndex right = m_tableModel->index(i, columnCount - 1);
                  //	selection.merge(QItemSelection(left, right), QItemSelectionModel::Select);
                  //}
                  
                  // this code works as well as Ctrl + A
                  selection.merge(QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1)), QItemSelectionModel::Select);
                  
                  QItemSelectionModel * sm = m_tableView->selectionModel();
                  sm->select(selection, QItemSelectionModel::Select);
                  

                  Besides, the QTableView has thousands of rows. The rows need to be selected could change at any time.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @NextSaturday said in Performance problem about QItemSelectionModel:

                  Using the code you suggested alone cannot realize the selection work. QitemSelection and QItemSelectionModel must be used at the same time.

                  I have never tried any of this but why must merge() be used? What is wrong with:

                  sm->select(QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1)), QItemSelectionModel::Select);
                  // or 
                  QItemSelection selection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1));
                  sm->select(selection, QItemSelectionModel::Select);
                  

                  ?

                  Even so it's hard to imagine calling QSelection::merge() --- and on an initially empty selection at that --- can be that slow? It's a bit of in-memory merging.

                  According to my test, it appears as if the number of times QItemSelection::merge() is called is the reason for the problem, not the count of "merged rows".

                  You certain how many times your code is called then?

                  Just how long does what take, what's the exact problem?

                  You just want to select many rows in a QTableView with a bound model? If that is slow, I can only guess that one of:

                  • redrawing
                  • copying content from model
                  • making model read more data

                  could be any kind of slow area?

                  BTW: depending on your table view selection settings you can allow all sorts of multiple selection from the keyboard. Use that to interactively select whatever you were trying to select programmatically. Now are you saying this does not have the performance from then on of you trying to do it in your code? If that is really the case then (unless someone else explains why) I might have a look at the Qt source code of selection from mouse/keyboard to see what it's doing.

                  N 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @NextSaturday said in Performance problem about QItemSelectionModel:

                    Using the code you suggested alone cannot realize the selection work. QitemSelection and QItemSelectionModel must be used at the same time.

                    I have never tried any of this but why must merge() be used? What is wrong with:

                    sm->select(QItemSelection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1)), QItemSelectionModel::Select);
                    // or 
                    QItemSelection selection(m_tableModel->index(0, 0), m_tableModel->index(rowCount - 1, columnCount - 1));
                    sm->select(selection, QItemSelectionModel::Select);
                    

                    ?

                    Even so it's hard to imagine calling QSelection::merge() --- and on an initially empty selection at that --- can be that slow? It's a bit of in-memory merging.

                    According to my test, it appears as if the number of times QItemSelection::merge() is called is the reason for the problem, not the count of "merged rows".

                    You certain how many times your code is called then?

                    Just how long does what take, what's the exact problem?

                    You just want to select many rows in a QTableView with a bound model? If that is slow, I can only guess that one of:

                    • redrawing
                    • copying content from model
                    • making model read more data

                    could be any kind of slow area?

                    BTW: depending on your table view selection settings you can allow all sorts of multiple selection from the keyboard. Use that to interactively select whatever you were trying to select programmatically. Now are you saying this does not have the performance from then on of you trying to do it in your code? If that is really the case then (unless someone else explains why) I might have a look at the Qt source code of selection from mouse/keyboard to see what it's doing.

                    N Offline
                    N Offline
                    NextSaturday
                    wrote on last edited by
                    #9

                    @JonB said in Performance problem about QItemSelectionModel:

                    If that is really the case then (unless someone else explains why) I might have a look at the Qt source code of selection from mouse/keyboard to see what it's doing.

                    Thanks very much for your patience.

                    I make a simple project to test this problem. The QT source code can be downloaded at the following github link.
                    link text

                    The project is a QWidget with a QTableView and a QPushButton. The QTableView has 10,000 rows. Press the button will trigger to select all rows of the QTableView by function selectAll().
                    The function selectAll() has 4 methods to realize the selection work. Methods 1 & 2 will lead to lag. Methods 3 & 4 works as well as Ctrl + A.

                    Specifically, if Method 1 or 2 is chosen, the application will be very slow when you tried to move/maximize/minimize the window.

                    1 Reply Last reply
                    0
                    • ruphusR Offline
                      ruphusR Offline
                      ruphus
                      wrote on last edited by ruphus
                      #10

                      May I suggest to use selection.select(left, right) instead of merging?
                      In my experience, the former is faster.

                      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