Results 1 to 2 of 2

Thread: qtableview selectrow(s)

  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 qtableview selectrow(s)

    Hello,

    I have a tableview / sqlmodel.
    A selection in the table can be made to rename those items with a dialog.

    Renaming the items in the sql database is done by QSqlquery and afterwards the model gets a select() so the data is up to date, this works fine.

    But I want to retain the selection after the select().
    I am trying to do a loop with the (new because the tabel is sorted and positions can change) rownumbers and selectRow().
    Qt Code:
    1. for (int i = 0; i < newnames.count(); i++)
    2. table->selectRow(findSong(newnames[i]));
    3. //findSong is code of my own to get the correct rownumber
    To copy to clipboard, switch view to plain text mode 
    But the result is that only the last row is selected allthough the table has:

    setSelectionBehavior(QAbstractItemView::SelectRows );
    setSelectionMode(QAbstractItemView::ExtendedSelect ion);

    Why does that loop with selectRow() only select the last row and what can I do about it? I looked at QAbstractItemView::setSelection() but that one wants a rect.

    Thanks
    Last edited by JeanC; 8th May 2008 at 14:55.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: qtableview selectrow(s)

    If you call select() the whole model is reset. I suggest you do the update manually using the model interface (QAbstractItemModel::setData()). Then your selection will be kept. And it will be much simpler to do, because you'll be operating on model indexes directly and not mapping them to database rows back and forth.

    Qt Code:
    1. QStringList newnames;
    2. newnames << ...
    3. int curr = 0;
    4. foreach(QModelIndex index, view->selectionModel()->selectedIndexes()){
    5. view->model()->setData(index, newnames[curr++], Qt::DisplayRole);
    6. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  2. QTableView currentChanged <> selecting header
    By Everall in forum Qt Programming
    Replies: 4
    Last Post: 1st April 2009, 08:24
  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. QTableView focus issue
    By gemidjy in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2008, 15:51
  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.