Results 1 to 7 of 7

Thread: QListWidgetItem check/uncheck

  1. #1
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QListWidgetItem check/uncheck

    Hello,

    I have a QListWidget with QAbstractItemView::ContiguousSelection and checkable items.

    What I would like to do is to multiple select a couple of items and then check/uncheck them. Unfortunately, the selection of all items changes first and then the checkstate is updated (so I first recieve a selectionChanged and then a itemChanged).

    Does anyone have a smart suggestion on how to change the behavior like this:

    - If I hit the checkbox I don't want my selection state(s) to be changed
    or (if not possible)
    - If I do not hit the checkbox, I want my itemChanged event first and then a selectionChanged event

    Regards,
    Arthur

  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: QListWidgetItem check/uncheck

    I think a common way to handle check state changes of multiple items is to provide an "external" widget for that purpose.
    You could for example provide a checkbox (outside the list widget) and/or a context menu action for changing the checkstate of selected items.

    Otherwise you might have to catch mouse click events (and possible some others too) to prevent the "view" to perform it's normal behaviour..
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidgetItem check/uncheck

    An external widget to check/uncheck all is of course handy, but what I want is multiple uncheck/check when I have items selected. I couldn't find a way of fixing this (I can't even find a checkbox widget based on mouseclick coordinates).

    Is there someone who can help me out?

  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: QListWidgetItem check/uncheck

    Quote Originally Posted by Arthur
    Unfortunately, the selection of all items changes first and then the checkstate is updated (so I first recieve a selectionChanged and then a itemChanged).
    Did you notice that item selection is changed due to mouse press events and the check state is not changed until mouse release events..?

    Anyhow, as I mentioned in the earlier post, you have to catch certain mouse events in order to prevent the view from acting like it normally does.

    Try these steps to solve the problem:
    - Override both, mousePressEvent and mouseReleaseEvent. Basically you want to handle these events always when they occur over an item's checkbox, and otherwise pass them over to the base class to handle it's contiguous selection.
    - Whenever a mouse press event (or release event, whichever you prefer) occurs over an item's checkbox, change the check state of all selected items.

    The hardest part might be calculating whether a mouse press/release event's position is inside an item's checkbox. You can use QStyle for that.

    This is just for reference:
    Qt Code:
    1. #include <QApplication>
    2. #include <QListWidget>
    3. #include <QListWidgetItem>
    4. #include <QMouseEvent>
    5. #include <QStyle>
    6. #include <QStyleOptionViewItem>
    7. #include <QStyleOptionButton>
    8.  
    9. class ListWidget : public QListWidget
    10. {
    11. public:
    12. ListWidget(QWidget* parent = 0) : QListWidget(parent)
    13. {
    14. // contiguous selection mode
    15. setSelectionMode(QAbstractItemView::ContiguousSelection);
    16.  
    17. // add some dummy checkable example items
    18. for (int i = 0; i < 10; ++i)
    19. {
    20. QListWidgetItem* item = new QListWidgetItem(QString::number(i+i));
    21. item->setCheckState(Qt::Unchecked);
    22. addItem(item);
    23. }
    24. }
    25.  
    26. protected:
    27. void mousePressEvent(QMouseEvent* event)
    28. {
    29. QListWidgetItem* item = selectedCheckStateItem(event->pos());
    30. if (item)
    31. {
    32. // mouse pressed over a selected items checkbox,
    33. // change the check states of all selected items
    34. setSelectedCheckStates(item->checkState() == Qt::Checked ? Qt::Unchecked : Qt::Checked);
    35. }
    36. else
    37. {
    38. // otherwise let the base class handle mouse press
    39. QListWidget::mousePressEvent(event);
    40. }
    41. }
    42.  
    43. void mouseReleaseEvent(QMouseEvent* event)
    44. {
    45. // do nothing if mouse released over a selected items checkbox
    46. if (!selectedCheckStateItem(event->pos()))
    47. {
    48. // otherwise let the base class handle mouse release
    49. QListWidget::mouseReleaseEvent(event);
    50. }
    51. }
    52.  
    53. private:
    54. // returns the selected item whose checkbox lies under 'pos'
    55. // returns 0 if not selected, no item at pos, or does not fit inside checkbox
    56. QListWidgetItem* selectedCheckStateItem(const QPoint& pos)
    57. {
    58. QListWidgetItem* item = itemAt(pos);
    59. if (item)
    60. {
    61. // with the help of styles, check if checkbox rect contains 'pos'
    62. opt.QStyleOption::operator=(viewOptions());
    63. opt.rect = visualItemRect(item);
    64. QRect r = style()->subElementRect(QStyle::SE_ViewItemCheckIndicator, &opt);
    65. // assure that the item is also selected
    66. if (selectedItems().contains(item) && r.contains(pos))
    67. {
    68. return item;
    69. }
    70. }
    71. return 0;
    72. }
    73.  
    74. // sets the check state of all selected items to 'checkState'
    75. void setSelectedCheckStates(Qt::CheckState checkState)
    76. {
    77. foreach (QListWidgetItem* item, selectedItems())
    78. item->setCheckState(checkState);
    79. }
    80. };
    81.  
    82. int main(int argc, char *argv[])
    83. {
    84. QApplication a(argc, argv);
    85. ListWidget w;
    86. w.show();
    87. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    88. return a.exec();
    89. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. The following 2 users say thank you to jpn for this useful post:

    nielsenj (19th May 2006), tpf80 (13th November 2009)

  6. #5
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidgetItem check/uncheck

    Thanks, I spent hours on finding out how to determine the click was in a checkbox (I hoped that I could get a 'child' widget from a certain position in the QListView...)

    I'll check out your code!

  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: QListWidgetItem check/uncheck

    Wouldn't it be easier to use an external widget as suggested and when it is activated, check the selection of the list widget and mark all selected items as checked?

    It should be as easy as:

    Qt Code:
    1. void someClass::someSlot(){
    2. foreach(QListWidgetItem *item, listwidget->selectedItems())
    3. item->setCheckedState(Qt::Checked);
    4. }
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    Jan 2006
    Location
    Leiden, the Netherlands
    Posts
    43
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QListWidgetItem check/uncheck

    It is easier indeed.

Similar Threads

  1. QListWidgetItem in Drag n' drop
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 3rd April 2009, 15:43
  2. Replies: 1
    Last Post: 12th October 2008, 20:02
  3. Check Box problem
    By Seema Rao in forum Qt Programming
    Replies: 6
    Last Post: 30th November 2007, 20:32
  4. Problems subclassing QListWidgetItem
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2006, 10:27
  5. QListWidget add QListWidgetItem
    By fellobo in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2006, 20:37

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.