QTreeView + sort model + QStandardItemModel, the most efficient way to update the model?
-
To 1: Take a look here or better try the following:
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); tableView->setStyleSheet( QStringLiteral("QAbstractItemView::item{ margin: 0; padding: 0; }") );
To 2: What's wrong with that? Same applies to QTreeView. This depends on the selection model used. You can simply set behaviour:
tableView->selectionModel().setSelectionBehaviour( QAbstractItemView::SelectRows );
Btw.: I don't want to confuse you, but do you know there's a
QDirModel
prepared for you? This seems pretty much like what you're looking for. Found a nice tutorial. -
Very interesting, I'll try your suggestions, shortly, thanks.
On 2: I have set selection behavior to "Select rows" for the view, but not the model. It worked for tree view, but not for table view, so it confused me.I have heard of the
QDirModel
. I'm making a file manager, so I need a flexible and powerful solution. Which means I have to write it all myself. Besides, I need to separate the model (UI level code) from the actual data (core level). -
Wait,
QItemSelectionModel
doesn't have asetSelectionBehaviour
member! So I can't dotableView->selectionModel().setSelectionBehaviour( QAbstractItemView::SelectRows );
-
Another observation: doing
verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); setStyleSheet("QAbstractItemView::item{ margin: 0; padding: 0; }");
Totally kills performance. Looks like the view does a LOT of relayouting, or maybe just calculating font metrics for each item. Something like that, judging from the call stack. Either way, the UI thread is 100% busy and the event loop is barely executing.
-
Wait,
QItemSelectionModel
doesn't have asetSelectionBehaviour
member! So I can't dotableView->selectionModel().setSelectionBehaviour( QAbstractItemView::SelectRows );
-
Another observation: doing
verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); setStyleSheet("QAbstractItemView::item{ margin: 0; padding: 0; }");
Totally kills performance. Looks like the view does a LOT of relayouting, or maybe just calculating font metrics for each item. Something like that, judging from the call stack. Either way, the UI thread is 100% busy and the event loop is barely executing.
@Violet-Giraffe Just try without setting resize mode.
Further, the css rule might not be right. Sorry, you need to figure that out by yourself.
-
Wait, QItemSelectionModel doesn't have a setSelectionBehaviour member! So I can't do
Right, but QAbstractItemView has:
tableView->setSelectionBehaviour( QAbstractItemView::SelectRows );
@antis said:
Right, but QAbstractItemView has:
tableView->setSelectionBehaviour( QAbstractItemView::SelectRows );
That it does! And like I said, I have set this option. the table view does select a whole row, but it does not span the cursor across the whole row. While the tree view does both no problem.
-
@antis said:
Right, but QAbstractItemView has:
tableView->setSelectionBehaviour( QAbstractItemView::SelectRows );
That it does! And like I said, I have set this option. the table view does select a whole row, but it does not span the cursor across the whole row. While the tree view does both no problem.
That it does! And like I said, I have set this option. the table view does select a whole row, but it does not span the cursor across the whole row. While the tree view does both no problem.
Got me. The dotted rectangle's shows the editing focus. Logically, the focus lies always on the focused table cell. In a QTreeView on the other hand, it lies on the the tree item. You can paint the focus rect over the whole table row by overriding "QTableView::paint".
-
First, the table view is what you want for your use case. So please don't switch the view.
Ok, I did some further research:
My default file manager (and the file dialogs itself) display the focus around the item in the first column. This makes total sense, as this is the only editable column (the file name). The flag is defined inQAbstractTableModel::flags
, which you need to override:Qt::ItemFlags MyTableModel::flags(const QModelIndex & index) const { if (index.column() > 0) { // remove focusable flag for all columns > 0 return QAbstractTableModel::flags(index) & ~Qt::ItemIsEditable; } return QAbstractTableModel::flags(index); }
EDIT: Corrected the code snippet.
-
Thanks. This is counter-intuitive, but as long as I can fix it reliably for all platforms with 3 lines of code - why not. I've already reverted the change from
QtreeView
toQTableView
under the pressure of the issues, but it won't take too long to try again.Filling
QTableView
is about twice as fast asQtreeView
, so that's a good incentive, but theQtreeView
definitely looks better on Windows (more native). At least so far. For example, the selection style is much different.