Results 1 to 9 of 9

Thread: Get list of selected rows from QTableView

  1. #1
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Get list of selected rows from QTableView

    win32:qt4.01

    I have a QTableView loaded with an QSqlTableModel. I have the selection method set to extended. I select multiple rows and have everything set to delete them but i just need to know which are selected. currentIndex() only gives the last selected item. Any ideas?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get list of selected rows from QTableView

    Qt Code:
    1. QSelectionModel *selections = tableView->selectionModel();
    2. QModelIndexList selected = selections->selectedIndexex();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get list of selected rows from QTableView

    ok that works sorta.

    This is what i have
    Qt Code:
    1. QItemSelectionModel *selections = ui.tableView->selectionModel();
    2. QModelIndexList selected = selections->selectedIndexes();
    3.  
    4. for( i = 0 ; i < selected.size(); i++ )
    5. {
    6. rowid = selected[i].row();
    To copy to clipboard, switch view to plain text mode 
    but size() doesn't return the correct number of selected items.

  4. #4
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get list of selected rows from QTableView

    whoops!

    QModelIndexList is filled with (rows*columns).

    How do you just get the rows that have been selected?

  5. #5
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get list of selected rows from QTableView

    Now i'm running into the problem that when i remove a row using m_TableModel->removeRow( rowid ) it screws up the rest of the removes that i want to do. I can't use removeRows() because that doesn't work for non-adjacent selections.

    I could switch to making an sql query for delete but that would sort of defeat the purpose of having a model

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Get list of selected rows from QTableView

    Check those indexes. Each index has a row and column number in it. Besides, if you set your model (or view? I don't remember right now), it'll only allow selecting whole rows, so it should be easy to find row numbers (for example ignore every index with column number not equal to 0, this way you'll be processing only 0-th columns, and the number of such items will be the number of rows selected).

  7. #7
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get list of selected rows from QTableView

    I had the same problem lately, and found maps very convenient for the job:

    Qt Code:
    1. void ALocalvalueTableView::removeSelected()
    2. {
    3. QMap<int, int> rows;
    4. foreach (QModelIndex index, selectedIndexes())
    5. rows.insert(index.row(), 0);
    6. QMapIterator<int, int> r(rows);
    7. r.toBack();
    8. while (r.hasPrevious()) {
    9. r.previous();
    10. model()->removeRow(r.key());
    11. } // while
    12. } // removeSelected
    To copy to clipboard, switch view to plain text mode 

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

    jnk5y (16th March 2006)

  9. #8
    Join Date
    Feb 2006
    Posts
    21
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get list of selected rows from QTableView

    So you use map to go to the end of the list and remove them from highest to lowest rowid? Sounds like a plan. Thanks for all the info.

  10. #9
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Get list of selected rows from QTableView

    The nice thing about the map is that it automaticly eliminates duplicates. The reverse delete order is important
    Last edited by seneca; 17th February 2006 at 17:06.

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  3. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  4. make QTableView work as a multi-column list view
    By wesley in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2008, 14:43
  5. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 14:29

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.