Results 1 to 9 of 9

Thread: How select next row in QTableView

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb 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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How select next row in QTableView

    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How select next row in QTableView

    You can try with setCurrentIndex in a for cycle like this:

    Qt Code:
    1. for (int row = 0; row < numRows; row++) {
    2. const QModelIndex index = model->index(row, 1);
    3. table->setCurrentIndex(index);
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How select next row in QTableView

    Quote Originally Posted by grub87 View Post
    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?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How select next row in QTableView

    Ok you have to do something in the for cycle:

    Qt Code:
    1. for (int row = 0; row < numRows; row++) {
    2. const QModelIndex index = model->index(row, 1);
    3. table->setCurrentIndex(index);
    4. "Do something here for the query"
    5. }
    To copy to clipboard, switch view to plain text mode 

    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

  6. #6
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb 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

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

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

  8. The following user says thank you to Lykurg for this useful post:

    estanisgeyer (17th June 2009)

  9. #8
    Join Date
    Jun 2009
    Location
    México
    Posts
    20
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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???

  10. #9
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How select next row in QTableView

    Quote Originally Posted by grub87 View Post
    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):

    Qt Code:
    1. Before deleting After
    2.  
    3. ... ...
    4. #3 --> #4
    5. #4 ....
    6. ...
    To copy to clipboard, switch view to plain text mode 

    You see, the "next row" will be on the place you have deleted the row. Hope this is clearer now...

Similar Threads

  1. Replies: 2
    Last Post: 7th June 2009, 10:47
  2. Select row in QTableView after insert.
    By fede in forum Newbie
    Replies: 1
    Last Post: 14th April 2009, 15:18
  3. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  4. How to select a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 6
    Last Post: 6th February 2008, 13:20
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.