Results 1 to 3 of 3

Thread: iterating selected rows in a qtableview

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default iterating selected rows in a qtableview

    I want to act on the selected rows in a qtableview.

    I'm trying the code below, however, list.count() gives the amount of selected cells in stead of selected rows, I only need the rowcount. How do I get that please, I searched but seems I'm too dumb to find it.

    Qt Code:
    1. const QModelIndexList list = table->selectionModel()->selection().indexes();
    2. for (int i = 0; i < list.count(); i++)
    3. {
    4. QModelIndex index = list.at(i);
    5. int row = index.row();
    6. QString name = model->record(row).value("name").toString();
    7. qDebug() << i << row << name;
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: iterating selected rows in a qtableview

    If you used "select rows" selection behavior you could simply use QItemSelectionModel::selectedRows(). If not, you could use for example a set:

    Qt Code:
    1. QSet<int> rows;
    2. const QModelIndexList list = table->selectionModel()->selection().indexes();
    3. for (int i = 0; i < list.count(); i++)
    4. {
    5. QModelIndex index = list.at(i);
    6. rows.insert(index.row());
    7. }
    8. qDebug() << rows;
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: iterating selected rows in a qtableview

    Quote Originally Posted by jpn View Post
    If you used "select rows" selection behavior you could simply use QItemSelectionModel::selectedRows(). If not, you could use for example a set:
    I have that behaviour yes, gee why didn't I see that.
    Quote Originally Posted by jpn View Post
    Qt Code:
    1. QSet<int> rows;
    2. const QModelIndexList list = table->selectionModel()->selection().indexes();
    3. for (int i = 0; i < list.count(); i++)
    4. {
    5. QModelIndex index = list.at(i);
    6. rows.insert(index.row());
    7. }
    8. qDebug() << rows;
    To copy to clipboard, switch view to plain text mode 
    Thanks for both.

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, 02:49
  2. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 13:49
  3. Copying QTableView rows
    By derrickbj in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 01:00
  4. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 07:55
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 17:59

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.