Performance problem about QItemSelectionModel
-
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?
-
@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.
-
@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. -
@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. -
This post is deleted!
-
This post is deleted!
-
@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
andQItemSelectionModel
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.
-
@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.
-
@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 textThe 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.