Results 1 to 9 of 9

Thread: QTableView get selected row

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTableView get selected row

    QT:4.1.1

    Hello everybody,

    Which function should i use to get my selected row?
    I need this to insert here:
    Qt Code:
    1. int myRow = tableView->???(); //i should get my selected Row as integer
    2. model->removeRows(myRow,1);
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  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: QTableView get selected row

    Through view's selection model:
    QItemSelectionModel * QAbstractItemView::selectionModel () const
    Returns the current selection.

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView get selected row

    Hi JPN!
    Should this return me a integer value?Have you see somewhere a example?I have pain for construct it
    Think DigitalGasoline

  4. #4
    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: QTableView get selected row

    Quote Originally Posted by raphaelf
    Should this return me a integer value?Have you see somewhere a example?I have pain for construct it
    Did you even try?

    Anyway, for a row index as an integer you can use something like this:
    Qt Code:
    1. // Get all selections
    2. QModelIndexList indexes = view->selectionModel()->selection().indexes();
    3. for (int i = 0; i < indexes.count(); ++i)
    4. {
    5. QModelIndex index = indexes.at(i);
    6. // To get the row/column numbers use index.row() / index.column()
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableView get selected row

    hI jpn!
    I cant see where should i use index.row(); ?
    It's a great pity that QTableView has not a function for that

    for count selected rows work:
    Qt Code:
    1. QModelIndexList indexes = ui.tableView->selectionModel()->selection().indexes();
    2. for (int i = 0; i < indexes.count(); ++i)
    3. {
    4.  
    5. QModelIndex index = indexes.at(i);
    6. QMessageBox::information(this,"", QString::number(i));
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 
    Think DigitalGasoline

  6. #6
    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: QTableView get selected row

    The selection list contains an index model per every selected item (cell in a table model).
    If you had selected a single row, for example, the list would contain an item per each cell on that row.
    Depending on the selection mode and behaviour of your view, the selection list contents may vary a lot, of course.

    So you wanted to get the "selected row". Then I suppose the selection behaviour is SelectRows and selection mode is SingleSelection..
    If so, due to the fact that all indexes in the selection list would represent different cells in a same row, you could just ask index.row() from the first index in the list and ignore others..

  7. The following user says thank you to jpn for this useful post:

    postb99 (20th February 2014)

  8. #7
    Join Date
    Feb 2006
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView get selected row

    Hello,

    if you don't want to mess around with Qts interview classes you might also intercept the tree's selection changed signal to your slot..

  9. #8
    Join Date
    Dec 2010
    Posts
    20
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView get selected row

    Hi raphaelf

    Maybe this will help u

    Qt Code:
    1. QModelIndexList selectedList = ui->tabel->selectionModel()->selectedRows();
    2. for( int i=0; i<selectedList.count(); i++)
    3. QMessageBox::information(this,"", QString::number(selectedList.at(i).row()));
    To copy to clipboard, switch view to plain text mode 

  10. The following 3 users say thank you to rivci for this useful post:

    postb99 (20th February 2014), RoyBellingan (12th July 2013), sattu (5th October 2011)

  11. #9
    Join Date
    Aug 2012
    Posts
    2
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QTableView get selected row

    The last suggestion is true if the selection mode is "selectrows"
    Qtableview is default to selectitems.

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 13:49
  2. QTableView only showing data when row selected
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2009, 06:34
  3. How to display selected columns in QTableView widget.
    By kaushal_gaurav in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:30
  4. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 15:29
  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.