PDA

View Full Version : Select a row from QTreeView



vieraci
10th July 2009, 01:42
I have a QTreeView with the model set from a QSqlQueryModel.
Now I need to select the first row when the query is run.
I can't find any way to do this programatically.
QTreeWidget has setCurrentItem() and some other means, but QTreeView and QSqlQueryModel don't seem to have any methods for selection. Do I have to resort to using a QTreeWidget ?

BTW: Found this in the docs, but it doesn't select anything:

QModelIndex first = model->index(0, 0, QModelIndex());
treeView->setCurrentIndex(first);

vieraci
10th July 2009, 05:33
Yes the code DOES work, it was in the wrong order (before I ran the query) :D

Pradeep V
6th December 2015, 10:01
Hi vieraci,

Can you please show me the snippet of code which is working?
I am facing the similar issue.


Thanks

d_stranz
6th December 2015, 17:54
I am sure that vieraci has been waiting for 6 1/2 years for someone to ask that.

QtCau
7th December 2015, 15:10
QPoint p(0,27 * [RowNumber]);
ui->tree_Widget->setCurrentIndex(ui->tree_Widget->indexAt(p));


You can use this. Its not perfect, but it works. Maybe you have to change the 27.

d_stranz
7th December 2015, 19:44
You can use this. Its not perfect, but it works. Maybe you have to change the 27.

Sorry, but that is not a good answer. It depends on font size, row height, and any number of other variables unique to the PC where the app is running. On your PC, maybe the height of a row is 27, but on my PC, it probably isn't. The "indexAt( const QPoint & )" method is generally used to let the application know where a user clicked with the mouse, not in the way you are suggesting.

The answer in the original post is correct and should be used.