Results 1 to 17 of 17

Thread: QTableView Item Selection

  1. #1
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView Item Selection

    When building a QTableView, I have noticed that the selection of a row or column in the ExtendedSelection mode remains in effect even when an element in that row or column is clicked. The current item changes; the selection does not. I've tried to use the clicked() SIGNAL to implement a change but it does not fire into a test SLOT. Is there any workaround for this situation? (I have a custom model, but even the demo examples exhibit this behavior.)

  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: QTableView Item Selection

    You can change selections through QItemSelectionModel instance that is linked to your view. Note though that the behaviour you are observing is the intended one for the selection mode you have chosen.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    johnmauer (24th January 2010)

  4. #3
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    Thanks. I did exactly as you suggested but had to use the low level mouse signals; the table clicked() SIGNAL doesn't seem to send. The selection behavior works fine in general, but leaves a lot to be desired for a single column table. The user can select the column, but never unselect it. (I know, use a list, but I really like the vertical header for a column of 10000 numbers)

  5. #4
    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: QTableView Item Selection

    I have no problems with unselecting selected rows for a single column table view with selection mode set to Extended. Maybe there is something wrong with your code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    What I could not do was unselect the single column once selected by clicking on an item as the user would likely do. I am using Extended mode specifically, not by default. I can select different rows, but not unselect the row when clicking on the same row item either. (In either case, a drag will deselect using the rubber band.)

    However, I did as you suggested and modified the behavior with the low level mouse SLOT and the QItemSelectionModel; it works fine now.

  7. #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: QTableView Item Selection

    Could you open Qt Designer, place a QTableWidget inside a form, add a column and few rows to it, set the appropriate selection mode, preview the form and see if it works then?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    Done. One QTableWidget in a dialog with one column and 7 rows, enter data in each row, select column, then clicking an item does not deselect the column. I used QtDesigner in Visual Studio with all defaults untouched, the columns and rows built in QtDesigner and viewed in Preview mode. Same behavior in compile and execution.

    Sorry for the delay: family day.

  9. #8
    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: QTableView Item Selection

    How many items did you select before trying to change selection by clicking on the item? Extended selection works in such a manner that it deselects all rows selected and selects the ones you click or drag. So if you have one row selected and you click on the same row again it won't be deselected. You need to Ctrl+click on it. If you want a different behaviour then use MultiSelection instead of ExtendedSelection.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    When the user selects an item in the usual way, the selection is cleared and the new item selected.
    The documentation suggests clearing the selection if Ctrl or Shift keys are not pressed. But the behavior appears to be the lack of item selection when the table has a single column. At the beginning, selecting an item also selects a row. If the column is then selected, selecting an item is always in the same column and the column does not deselect. In any event, the correct behavior is restored through the mousePressed SLOT and clearing the selection.

  11. #10
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Item Selection

    Quote Originally Posted by johnmauer View Post
    The documentation suggests clearing the selection if Ctrl or Shift keys are not pressed. But the behavior appears to be the lack of item selection when the table has a single column. At the beginning, selecting an item also selects a row. If the column is then selected, selecting an item is always in the same column and the column does not deselect. In any event, the correct behavior is restored through the mousePressed SLOT and clearing the selection.
    I don't understand what number of columns has to do with selection in this case... In documentation sentence quoted by you it is clearly said that when you click on item then selection is cleared and the new item selected. So if you click on the selected item then even if the item was selected it would be reselected - so there is no chance to deselect selected item by just clicking on it.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. #11
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    In short, using the Extended mode, if a column has been selected by clicking the header, then clicking an item in that column does not deselect the column before it selects the item. The only ways to deselect the column are to click the row header which selects a row or drag a selection. I wasn't talking about selecting the same item, but selecting the column (which sets row 0 as the current item), then clicking another item. That should deselect the column and select the item. Currently, it leaves the column selected, and moves only the current item.

  13. #12
    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: QTableView Item Selection

    That's not how it works on my computer... I click the header and the whole column gets selected. I click on one of the items in the column and the column gets deselected and the item gets selected. If it doesn't work for you this way then maybe that's a platform dependent behaviour and it is intended to work this way on Windows.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    Maybe. In any case the following code seems to solve the behavior and make what you must see on another platform.

    Qt Code:
    1. void DataTable::mousePressEvent(QMouseEvent *event)
    2. {
    3.  
    4. if ((event->button() & Qt::LeftButton) &&
    5. !(event->modifiers() & Qt::ShiftModifier) &&
    6. !(event->modifiers() & Qt::ControlModifier))
    7. {
    8. selectionModel()->clearSelection();
    9. }
    10. QAbstractItemView::mousePressEvent(event);
    11. }
    To copy to clipboard, switch view to plain text mode 

  15. #14
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTableView Item Selection

    hmm I have Windows 7 and I checked that when I select the column by clicking header and then click one item then only this one item is selected. What Windows and Qt version do you have?
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  16. #15
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    I'm running Windows XP Professional SP3, Visual Studio 2008, with Qt 4.6.0 (commercial license) and the VS addin. I'll try to bring up a Windows 7 system with Qt if the licenses will let me.

    Edit Note: I loaded Qt 4.6.1 and the behavior is changed, but on mouseReleased. I went back and verified with 4.6.0. Makes sense now why you didn't see it.
    Last edited by johnmauer; 25th January 2010 at 22:06.

  17. #16
    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: QTableView Item Selection

    I was using 4.5.3.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #17
    Join Date
    Dec 2009
    Posts
    47
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView Item Selection

    Weird. I have attached a jpg of the test table in 4.6.0 where the column header was checked, then item 3. As you can see, item 3 is the current item , but the column is still selected. As I noted, in 4.6.1, this was fixed (again?).

    Thanks a lot for your help, both of you. I can see why you were skeptical.
    Attached Images Attached Images

Similar Threads

  1. Replies: 2
    Last Post: 26th November 2009, 05:45
  2. Rubberband Item Selection
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 23rd October 2009, 09:48
  3. Item Selection in QListWidget
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 12th February 2008, 16:53
  4. QTableView row selection
    By davemar in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2007, 15:39
  5. Replies: 14
    Last Post: 9th November 2006, 09:35

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.