Results 1 to 13 of 13

Thread: How to make QAbstractItemModel 's data checkable

  1. #1
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question How to make QAbstractItemModel 's data checkable

    Hi,
    I want to make some data of QAbstractItemModel to be user-checkable. It seems that could be done via QAbstractItemModel : : flags(index), however, how to change the model's flags?

    the QStandardItemModel has the interface setCheckable(bool) to make an item checkable or not, but the QAbstractItemModel does not.

    By now, i can get the flags by
    Qt Code:
    1. bool b = index.flags() & Qt::ItemIsUserCheckabel;
    To copy to clipboard, switch view to plain text mode 

    but how to set it?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    You will need to save flags in your own defined role.
    Thats how standard item does it. You can have a look at QStandardItem::setFlags code.

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

    nifei (4th February 2009)

  4. #3
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    Quote Originally Posted by aamer4yu View Post
    You will need to save flags in your own defined role.
    Thats how standard item does it. You can have a look at QStandardItem::setFlags code.
    Yes i can save the flags but how to set them to QAbstractitemModel, which has not the similar interface to control the flags?

  5. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    when you set data using Qt::CheckStateRole a checkbox will appear in item, but you also need to set flags as aamer4yu said.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #5
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    Quote Originally Posted by spirit View Post
    when you set data using Qt::CheckStateRole a checkbox will appear in item, but you also need to set flags as aamer4yu said.
    When i set data via Qt::CheckStateRole the item becomes user - checkable automatically. However I have to manage QAbstractItemModel instead of QStandardItemModel, then how to change the flags via index as there is no item in QAbstractItemModel?

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    When i set data via Qt::CheckStateRole the item becomes user - checkable automatically.
    set it unchecked

    However I have to manage QAbstractItemModel instead of QStandardItemModel, then how to change the flags via index as there is no item in QAbstractItemModel?
    you have to reimplement Qt::ItemFlags QAbstractItemModel::flags ( const QModelIndex & index ) const
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Oct 2008
    Location
    Beijing China
    Posts
    77
    Thanks
    21
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: How to make QAbstractItemModel 's data checkable

    Quote Originally Posted by aamer4yu View Post
    You will need to save flags in your own defined role.
    Thats how standard item does it. You can have a look at QStandardItem::setFlags code.
    Qt Code:
    1. void QStandardItem::setFlags(Qt::ItemFlags flags)
    2. {
    3. setData((int)flags, Qt::UserRole - 1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    got it, thanks a lot.

  9. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    Quote Originally Posted by nifei View Post
    Qt Code:
    1. void QStandardItem::setFlags(Qt::ItemFlags flags)
    2. {
    3. setData((int)flags, Qt::UserRole - 1);
    4. }
    To copy to clipboard, switch view to plain text mode 

    got it, thanks a lot.
    btw, you can use void QStandardItem::setCheckable ( bool checkable ) insted of setting flags in that case if you use QStandardItem.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. The following user says thank you to spirit for this useful post:

    nifei (4th February 2009)

  11. #9
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows Symbian S60

    Default Re: How to make QAbstractItemModel 's data checkable

    Can you explain how what you posted fixed your problem if you’re still around? I’m having the same problem making QAbstractItemModel 's data checkable.
    I can set the checked/unchecked state in data() but I can’t set the checkbox to a checkable state. I added Qt::ItemIsUserCheckable to flags() but this does nothing. How did what you posted:

    void QStandardItem::setFlags(Qt::ItemFlags flags)
    1. {
    2. setData((int)flags, Qt::UserRole - 1);
    3. }
    fix the problem?

    You were told to “save flags in your own defined role”. How does this work and how exactly were you able to do this? I’ve looked at the code for QStandardItem::setFlags() but it includes nothing about roles.

  12. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to make QAbstractItemModel 's data checkable

    Quote Originally Posted by DPB1956 View Post
    I can set the checked/unchecked state in data()
    Not sure why you're setting anything in data(). Anywho try something like the following.

    In data():
    Qt Code:
    1. . . .
    2. if(role == Qt::CheckStateRole){
    3. QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
    4. return item->checkState();
    5. }
    6. . . .
    To copy to clipboard, switch view to plain text mode 
    Then in setData():
    Qt Code:
    1. . . .
    2. if(role==Qt::CheckStateRole) {
    3. QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
    4. if(item->isChecked()) item->setCheckState(Qt::Unchecked);
    5. else item->setCheckState(Qt::Checked);
    6. return true;
    7. }
    8. return QAbstractItemModel::setData(index, value, role);
    9. }
    To copy to clipboard, switch view to plain text mode 
    HTH
    Last edited by norobro; 23rd August 2011 at 03:14. Reason: Removed some duplicate code

  13. The following 2 users say thank you to norobro for this useful post:

    Anjan@369 (1st April 2013), DPB1956 (23rd August 2011)

  14. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to make QAbstractItemModel 's data checkable

    Quote Originally Posted by DPB1956 View Post
    I’m having the same problem making QAbstractItemModel 's data checkable.
    I can set the checked/unchecked state in data() but I can’t set the checkbox to a checkable state. I added Qt::ItemIsUserCheckable to flags() but this does nothing.
    Does nothing in what way?

    QAbstractItemModel doesn't contain any data, it is an abstract interface that you can implement any way you like. When you create your implementation of the that interface you control how it behaves. If you want a view to allow checking of an item from your model then you need to return Qt::ItemIsUserCheckable amongst the flags() for the item (which should be editable). You will also need to do something sane with Qt::CheckStateRole in the implementation of data() and setData().

    If you want a function like QStandardItem::setCheckable() to apply check-ability on an item-by-item basis then you need to add the method to your implementation and find somewhere to store the check-ability flags against an item in your model implementation. One solution is to use another role on the item to store/retrieve the check-ability attribute (i.e. like QStandardItemModel does) but you could equally use another structure. How you do it is entirely up to you because only you know what the data being presented through the model looks like.

    Whether a view does anything with the checkable state is another question.

  15. The following user says thank you to ChrisW67 for this useful post:

    DPB1956 (23rd August 2011)

  16. #12
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Windows Symbian S60

    Default Re: How to make QAbstractItemModel 's data checkable

    Thanks for responding. After adding setData() the checkbox is now working properly. I was under the impression that setting Qt::ItemIsUserCheckable to flags() would work the same way as setCheckable(true) in QStandardItemModel where the checkbox could be toggled without setting any additional flags.

    Thanks for your help.

  17. The following user says thank you to DPB1956 for this useful post:

    Anjan@369 (1st April 2013)

  18. #13
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    2
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: How to make QAbstractItemModel 's data checkable

    I am still unable to set the flag as in my flag function i made it as Qt::ItemIsUserCheckable;
    but my got got crashed in data function at item->checkState();
    pls help me out how to set or save the flag in which function and how to use it...
    thnx in adv.

Similar Threads

  1. Best way to display lots of data fast
    By New2QT in forum Newbie
    Replies: 4
    Last Post: 16th October 2008, 22:46
  2. Compiling with Qmake/Make
    By VireX in forum Newbie
    Replies: 25
    Last Post: 22nd February 2007, 05:57
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

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.