Results 1 to 6 of 6

Thread: ItemIsUserCheckable and checking all selected items

  1. #1
    Join Date
    May 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default ItemIsUserCheckable and checking all selected items

    Hi,

    I have a subclassed QAbstractListModel attached to a QListView. I have setSelectionMode(ExtendedSelection)

    I would like to be able to select multiple rows, and then press the spacebar, and have it toggle the checkboxes on ALL of the selected rows.

    How can I make this happen?

    I was thinking of a hack in my model::setData() ... give the model the list's selectionModel so that it knows what other items are selected, and then the setData() method can do the work of toggling all the checks. But this seems to go against the class design...

    thanks,
    Paul

  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: ItemIsUserCheckable and checking all selected items

    Eeeem... no, that's a bad way of doing things

    Reimplement keyPressEvent for the view and inside iterate over the selection model of the view and call setData for each item with appropriate role and value.
    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. #3
    Join Date
    May 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ItemIsUserCheckable and checking all selected items

    How do I do that if the form has been designed in designer? Its a QListView...

  4. #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: ItemIsUserCheckable and checking all selected items

    Right click on it and choose "Promote to" and enter your subclass data. Alternatively you can install an event filter on the view instead of reimplementing it.
    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.


  5. #5
    Join Date
    May 2009
    Posts
    12
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: ItemIsUserCheckable and checking all selected items

    Excellent, thanks for that tip.

    I went with the eventFilter method, see below... I had to tell the event filter about the model, and the model has a toggle(QModelIndex) method which does the tick/unticking.

    The alternative was to call ListView::edit(idx), but that didn't work, or the protected edit(idx,trigger,event), but I thought that was too much to fudge, at least this way I know what code is going to be executed.


    Qt Code:
    1. class MultiTicker : public QObject
    2. {
    3. public:
    4. MultiTicker(QAbstractItemView * view, ListModel_Checkable * model) : QObject(view), view(view), model(model) {}
    5.  
    6. protected:
    7. bool eventFilter(QObject *obj, QEvent *event)
    8. {
    9. if (event->type() == QEvent::KeyPress)
    10. {
    11. QKeyEvent *keyEvent = dynamic_cast<QKeyEvent*>(event);
    12. switch (keyEvent->key())
    13. {
    14. case Qt::Key_Space:
    15. case Qt::Key_Enter:
    16. case Qt::Key_Return:
    17. case Qt::Key_Select: // I think this a Phone/PDA key, but never mind hopefully can't hurt
    18. {
    19. // get the current selection
    20. QModelIndexList sels = view->selectionModel()->selectedIndexes();
    21. // get the current index
    22. QModelIndex curr = view->currentIndex();
    23.  
    24. // are we a member?
    25. if (sels.contains(curr))
    26. {
    27. // for each selection, toggle the checkbox through
    28. // the usual listview::edit method.
    29. for (QModelIndexList::iterator s = sels.begin(); s != sels.end(); ++s)
    30. model->toggle(*s);
    31. return true;
    32. }
    33. }
    34.  
    35. default: ;
    36. }
    37. }
    38. // standard event processing
    39. return QObject::eventFilter(obj, event);
    40. }
    41.  
    42. private:
    43. ListModel_Checkable * model;
    44. };
    To copy to clipboard, switch view to plain text mode 

  6. #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: ItemIsUserCheckable and checking all selected items

    What do you mean about the edit() thing? You don't need to touch edit in any way. You just call setData() with Qt::CheckStateRole on the model for each index and that's it.
    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.


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.