PDA

View Full Version : How select next row in QTableView



estanisgeyer
17th June 2009, 18:29
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

spirit
17th June 2009, 18:35
take a look at QAbstractItemView::setCurrentIndex and QItemSelectionModel::select. access to QItemSelectionModel::select you can get using QAbstractItemView::selectionModel.

grub87
17th June 2009, 19:21
You can try with setCurrentIndex in a for cycle like this:




for (int row = 0; row < numRows; row++) {
const QModelIndex index = model->index(row, 1);
table->setCurrentIndex(index);
}

spirit
17th June 2009, 19:25
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?

grub87
17th June 2009, 19:43
Ok you have to do something in the for cycle:



for (int row = 0; row < numRows; row++) {
const QModelIndex index = model->index(row, 1);
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

estanisgeyer
17th June 2009, 19:48
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

Lykurg
17th June 2009, 20:05
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.

grub87
17th June 2009, 20:06
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???

Lykurg
17th June 2009, 21:02
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):



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...