How select next row in QTableView
Hi friends,
How to select the next row of a QTableView/QSqlQueryModel? I need to delete a record from the database and then run the SQL command, but I want to position the cursor to select this next record (row).
Thank you for your attention.
Marcelo E. Geyer
Re: How select next row in QTableView
Re: How select next row in QTableView
You can try with setCurrentIndex in a for cycle like this:
Code:
for (int row = 0; row < numRows; row++) {
table->setCurrentIndex(index);
}
Re: How select next row in QTableView
Quote:
Originally Posted by
grub87
You can try with setCurrentIndex in a for cycle like this:
and after that a cursor will be at numRows - 1 position, but these items will not be selected except last one. how it can help?
Re: How select next row in QTableView
Ok you have to do something in the for cycle:
Code:
for (int row = 0; row < numRows; row++) {
table->setCurrentIndex(index);
"Do something here for the query"
}
and run your SQL command once you are in the needed index, you can put an if clause here to know if it is the record you want to delete and also use index.data(). Hope this helps
Re: How select next row in QTableView
Hi,
Before deleting the record in the database I have to get the "id" of the next row in QTableView / QSqlQueryModel and when you call the SELECT again to go row by row what the row is that "id", since it is unique. If not, patience should not select any row but the first.
Thanks,
Marcelo E. Geyer
Re: How select next row in QTableView
As I understand your case, it is no problem at all: You want delete the current selected row. Cache that index, delete the row, update your view, and select the cached index. That "points" to the lower row, of which you have deleted. Of course only if you don't apply any other filters.
Re: How select next row in QTableView
I didn't understand the last thing. Let's say you want to delete record #3 of your databse but first know what is the id of record #4???
Re: How select next row in QTableView
Quote:
Originally Posted by
grub87
I didn't understand the last thing. Let's say you want to delete record #3 of your databse but first know what is the id of record #4???
You don't have to know the id of #4. It's sufficient to know the position (index) of #3. Why? (Under the axiom, you don't change the filter of the view or whatever):
Code:
Before deleting After
... ...
#3 --> #4
#4 ....
...
You see, the "next row" will be on the place you have deleted the row. Hope this is clearer now...