PDA

View Full Version : How to select a row in QTreeView



chithara
31st May 2019, 13:10
Hi,

I have a QTreeView derived from QAbstractItemModel.
I want to make the selection to the topmost node(root node) of the tree when the UI launches.

Below is my code, but no selection happening.

QModelIndex rootIndex = TreeView->rootIndex();
QModelIndex rootChildIndex = TreeModel->index(0, 0, rootIndex);

if (rootChildIndex.isValid())
{
TreeView->selectionModel()->select(rootChildIndex , QItemSelectionModel::Select);
}

anda_skoa
1st June 2019, 10:12
Add the QItemSelectionModel::Rows flag to the second argument if you want the whole row (all columns) selected.

Cheers,
_

chithara
3rd June 2019, 08:18
Not working! :(

TreeView->selectionModel()->select(rootChildIndex , QItemSelectionModel::Rows);

Ginsengelf
3rd June 2019, 11:52
Hi, you need to add QItemSelectionModel::Select, otherwise nothing will be changed.

TreeView->selectionModel()->select(rootChildIndex , QItemSelectionModel::Rows | QItemSelectionModel::Select);

Ginsengelf