Results 1 to 6 of 6

Thread: Checkboxes in QAbstractITemModel

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Checkboxes in QAbstractITemModel

    I've gotten some checkboxes displayed in a QTreeView, which has a model that is a subclass of QAbstractItemModel. The checkboxes show up fine for the column I want them to, but I would like to have the checkboxes unly become checked/unchecked when the user clicks on the checkbox itself. At the moment, the checkbox recieves click events when the user clicks in the column/row the checkbox is in. So the user only has to click on a row in column 0 in order for the checkbox to be toggled. how can I get Qt to check if the checkbox itself has been clicked?

  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: Checkboxes in QAbstractITemModel

    Quote Originally Posted by Valheru View Post
    I've gotten some checkboxes displayed in a QTreeView
    Are we talking about checkable items or real QCheckBoxes?

    but I would like to have the checkboxes unly become checked/unchecked when the user clicks on the checkbox itself.
    This is how checkable items work by default. See the attached example.
    Attached Files Attached Files
    J-P Nurmi

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

    bair (23rd March 2009)

  4. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Checkboxes in QAbstractITemModel

    They're checkable items, sorry. The model's data() function returns Qt::IsUserCheckable for column 0 in the model.

    I know how checkable items work in a QStandardItemModel, that is in fact exactly what I would like to achieve in my implementation. But that isn't how they are working.

    I've attached the source of a simple implementation. You'll need CMake to build it.
    Attached Files Attached Files

  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: Checkboxes in QAbstractITemModel

    If you do this:
    Qt Code:
    1. void ModelTestModel::clicked( const QModelIndex& index )
    2. {
    3. if( index.column() == 0 ){
    4. ModelTestModelItem *item = static_cast< ModelTestModelItem* >( index.internalPointer() );
    5.  
    6. if( item->checkState() == Qt::Checked )
    7. item->setCheckState( Qt::Unchecked );
    8. else
    9. item->setCheckState( Qt::Checked );
    10.  
    11. _parent->update( index );
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    then no wonder a click on the whole area makes the item checked. There is no need for such things. The checkable functionality is provided out of the box by the framework. For you it doesn't work, because you didn't implement setData(). If you do and handle changing the Qt::CheckStateRole, it will work as expected.

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

    Valheru (28th November 2007)

  7. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Checkboxes in QAbstractITemModel

    I'm having trouble getting the setData() function of the QAbstractItemModel to work. Currently it looks like this :

    Qt Code:
    1. bool ModelTestModel::setData( const QModelIndex &index, const QVariant& value, int role )
    2. {
    3. // qDebug( index );
    4. qDebug( value.toString().toLatin1() );
    5. qDebug( QString::number( role ).toLatin1() );
    6. // if( role != Qt::CheckStateRole )
    7. // return false;
    8.  
    9. ModelTestModelItem *item = static_cast<ModelTestModelItem*>( index.internalPointer() );
    10.  
    11. if( item->checkState() == Qt::Checked )
    12. item->setCheckState( Qt::Unchecked );
    13. else
    14. item->setCheckState( Qt::Checked );
    15.  
    16. // emit dataChanged( index, index );
    17. return true;
    18. }
    To copy to clipboard, switch view to plain text mode 

    But the debug lines are not appearing in the console. It seems as though it is never getting called. Why would that be? Doesn't it automatically get called internally by QAbstractItemModel whenever you click on a QModelIndex?

  8. #6
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Checkboxes in QAbstractITemModel

    nm, for some wierd reason I'd removed the Qt::ItemIsUserCheckable return from data() >_<. Once I'd put that back it was fixed. Thanks for the help.

Similar Threads

  1. Displaying windows depending on checkboxes
    By Salazaar in forum Newbie
    Replies: 1
    Last Post: 12th May 2007, 20:42
  2. Displaying QAbstractItemModel in a Table
    By bmesing in forum Qt Programming
    Replies: 1
    Last Post: 28th March 2007, 12:11
  3. QAbstractItemModel for dummies
    By morty in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 16:25
  4. Subclassing QAbstractItemModel
    By r366y in forum Qt Programming
    Replies: 2
    Last Post: 2nd May 2006, 09:46
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 21:16

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.