Select a row from QTreeView
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:
Code:
treeView->setCurrentIndex(first);
Solved: Select a row from QTreeView
Yes the code DOES work, it was in the wrong order (before I ran the query) :D
Re: Select a row from QTreeView
Hi vieraci,
Can you please show me the snippet of code which is working?
I am facing the similar issue.
Thanks
Re: Select a row from QTreeView
I am sure that vieraci has been waiting for 6 1/2 years for someone to ask that.
Re: Select a row from QTreeView
Code:
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.
Re: Select a row from QTreeView
Quote:
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.