以编程方式在QTreeView中选择一行 [英] Selecting a row in QTreeView programmatically
问题描述
我有一个以QFileSystemModel作为模型的QTreeView。
I have a QTreeView with QFileSystemModel as model.
QTreeView的SelectionBehavior设置为SelectRows。
The QTreeView has SelectionBehavior set to SelectRows.
在我的代码中,我读取了一个数据集以进行选择,然后通过以下方式进行选择:
In my code I read a dataset to select and then select them via:
idx = treeview->model()->index(search);
selection->select(idx, QItemSelectionModel::Select);
这将选择一个单元格,而不是该行。 。
This selects a cell, not the row . .
添加了一个愚蠢的解决方法,但宁愿以正确的方式解决此问题。
Have added a stupid workaround, but would rather fix this the correct way.
for (int col=0; col< treeview->model()->columnCount(); col++)
{
idx = treeview->model()->index(search, col);
selection->select(idx, QItemSelectionModel::Select);
}
还是这是唯一的^^方法?
Or is that ^^ the only way to do it?
推荐答案
还可以使用QItemSelection选择整行:
You can also select an entire row using an QItemSelection:
selection->select (
QItemSelection (
treeview->model ()->index (search, 0),
treeview->model ()->index (search, treeview->model ()->columnCount () - 1)),
QItemSelectionModel::Select);
此外,如果您还希望为用户点击选择行,则需要设置选择行为:
Also if you also want row selection for user clicks you need to set the selection behavior:
treeview->setSelectionBehavior (QAbstractItemView::SelectRows)
这篇关于以编程方式在QTreeView中选择一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!