Hiding the checkbox of a QListWidgetItem

Just something I figured out how to do, and it’s not in the top Google results:

The items in a QListWidget can be user-checkable, though by default they are not: QListWidget doesn’t display a (checked or unchecked) check box for a given item unless you set its check state to any value.

Once you set a check status, it’s seemingly impossible to remove the checkbox, even if you un-check it or use QListWidgetItem::setFlags() to remove the Qt::ItemIsUserCheckable flag.

The trick is to use QListWidgetItem::setData() to set the data in the Qt::CheckStateRole to an empty QVariant(). The same method can be used to set the item’s check state.

Unfortunately, this doesn’t preserve the check state, which is the reason why I investigated hiding the checkbox (I hoped that the state can be preserved while the box is hidden). Now I’ll probably store the original check state as user data.

Advertisements
This entry was posted in Programming, Qt and tagged . Bookmark the permalink.