Results 1 to 3 of 3

Thread: QTableWidget - Select multiple rows.

  1. #1
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QTableWidget - Select multiple rows.

    Hello,

    I am looking to select multiple rows in a tablewidget at once, as if the user had Ctrl+clicked or Shift-clicked on the vertical headers.

    I have the selection mode set to QAbstractItemView::ExtendedSelection and can perform this operation by clicking.

    Using selectRow(int) clears the previous selection. Looking through the source code, selectRow in QTableView calls QTableViewPrivate::selectRow(int, bool).

    My guess is that I would have to reimplement QTableView::selectRow() but I'm not quite sure how to do that to get the behaviour I want. I'm working on it in the mean time but if anyone has any suggestions that would be great.

    Edit:
    I am currently using Python and PyQt4 for most of my development but I am comfortable with C++ as well.
    Last edited by wally; 17th October 2008 at 19:30.

  2. #2
    Join Date
    Oct 2008
    Posts
    19
    Thanks
    6
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableWidget - Select multiple rows.

    Hello Everyone,

    Turns out I figured out how to do what I want. I don't know if this is the most elegant way to perform this action but it works. I'm just going to put the idea into a method called "selectRows" in my subclassed table.

    Turns out my problem was using QItemSelectionModel::Rows instead of QItemSelectionModel::Select.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtGui/QTableWidget>
    3. #include <QtGui/QAbstractItemView>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. QTableWidget w(10,3);
    9. QItemSelectionModel *selectionModel = w.selectionModel();
    10. w.selectRow(1);
    11. QItemSelection itemSelection = selectionModel->selection();
    12. w.selectRow(3);
    13. itemSelection.merge(selectionModel->selection(), QItemSelectionModel::Select);
    14. w.selectRow(5);
    15. itemSelection.merge(selectionModel->selection(), QItemSelectionModel::Select);
    16.  
    17. selectionModel->clearSelection();
    18. selectionModel->select(itemSelection,QItemSelectionModel::Select);
    19. w.show(); // ideally, upon show rows 2,4 and 6 should appear selected.
    20. // now, all three rows are selected.
    21. w.resize(350,400);
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget - Select multiple rows.

    what about using the designer go to the properties of the QtableWidget
    and for selectionbehavior , SelectRows is active.

  4. The following user says thank you to SunnySan for this useful post:

    George Neil (26th March 2010)

Similar Threads

  1. Copy row(s) from QTableWidget
    By allensr in forum Qt Programming
    Replies: 10
    Last Post: 1st February 2017, 08:59
  2. QTableWidget sorting with hidden rows
    By alex140773 in forum Qt Programming
    Replies: 0
    Last Post: 8th July 2008, 12:35
  3. Select columns from a QTableWidget
    By toglez in forum Qt Programming
    Replies: 10
    Last Post: 7th October 2007, 15:15
  4. QTableWidget (resizing rows, turning off selection, etc.)
    By kiss-o-matic in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2007, 01:57
  5. Replies: 6
    Last Post: 5th March 2006, 21:05

Tags for this Thread

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.