[Qt-interest] How to make a list of checkboxes

Andre Somers andre at familiesomers.nl
Mon May 31 09:30:42 CEST 2010


Op 31-5-2010 8:37, yogesh upreti schreef:
> Hallo Group,
> I have seen this question being ask a lot of times in the group, but 
> all the discussion I saw in mail archeive talks about QCheckListItem 
> which is ( I think) not available in Qt 4.6, as I couldn't find any 
> reference in documentation.
> So can someone please tell me how to make checkbox in QListView or 
> QListWidget? and also how to access checking and unchecking of Item.
When using QListView, you need to provide your own model (derived 
directly or indirectly from QAbstractItemModel). To make an item 
checkable, you need to return 1) the correct combination of flags from 
QAbstractItemModel::flags(), 2) make QAbstractItemModel::data() return 
appropriate data for Qt::CheckStateRole and 3) to store the checks, 
implement setData() for the same role. This is also where you can 
"access the checking and unchecking" of the item. You can, in your 
QAbstractItemModel subclass, create a signal that you emit if the 
checkstate is toggled.

> I tried it using QListWidgetItem with 
> item->setFlags(Qt::ItemIsUserCheckable), but doing this is changing my 
> item to somehow disabled item and not chowing any checkboxes.
>
Obviously. The flags is plural. That is: it is a combination of multiple 
flags that control different aspects of the look and feel of the item. 
If you only set Qt::ItemIsUserCheckable, you are not setting 
Qt::ItemIsEnabled... You need to set the correct *combination* of flags. 
You can do that by bitwise or-ing them together. Read the QFlags 
documentation for details.
Also, don't forget to set the actual value of the checkbox using 
QListWidgetItem::setData(Qt::CheckStateRole, QVariant(Qt::Checked));

André


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100531/1af57dcc/attachment.html 


More information about the Qt-interest-old mailing list