2 Attachment(s)
QItemDelegate and QCheckBox
Hello all,
I'm attempting to implement a checkbox delegate in my tree view, and I'm running into two problems:
1. The checkbox only appears when I attempt to "edit" a given cell. I'd rather it appear at all times.
2. When the checkbox does appear, it "overlays" the existing text, making it difficult to read. See the pictures below.
I'd appreciate any input on the above two problems. I can provide some code, but I suspect I'm having more of a concept problem than a code problem. Thanks!
Re: QItemDelegate and QCheckBox
You should make the items checkable instead of providing a combo box as an editor.
If you're using QTreeView + model:
If you're using QTreeWidget:
Code:
item->setFlags(item->flags() | Qt::ItemIsUserCheckable); // make checkable
item->setCheckState(Qt::Unchecked); // initialize check state to something valid
Re: QItemDelegate and QCheckBox
Ok, that works better visually... now how would I go about enabling the user to actually check the boxes? At the moment, I cannot check anything. Should I be listening on setData()? If so, which role should I be on the lookout for?
Re: QItemDelegate and QCheckBox
Quote:
Originally Posted by
KShots
Should I be listening on setData()? If so, which role should I be on the lookout for?
Yes, go for setData() and exactly the same role, Qt::CheckStateRole that is. :)
Re: QItemDelegate and QCheckBox
Thanks, that's exactly the behavior I was looking for :)
Re: QItemDelegate and QCheckBox
What if it were a QComboBox instead of a QCheckBox (or a custom widget for that matter). How would you display that in a persistent state? (QAbstractItemView allows for openPersistentEditor but that seems pretty cumbersome)