PDA

View Full Version : QItemDelegate and QCheckBox



KShots
26th July 2007, 00:23
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!

jpn
26th July 2007, 00:36
You should make the items checkable instead of providing a combo box as an editor.

If you're using QTreeView + model:

make the model return Qt::ItemIsUserCheckable (http://doc.trolltech.com/4.3/qt.html#ItemFlag-enum) amongst the other flags (http://doc.trolltech.com/4.3/qabstractitemmodel.html#flags)
make the model return valid data (http://doc.trolltech.com/4.3/qabstractitemmodel.html#data) for Qt::CheckStateRole (http://doc.trolltech.com/4.3/qt.html#ItemDataRole-enum)


If you're using QTreeWidget:


item->setFlags(item->flags() | Qt::ItemIsUserCheckable); // make checkable
item->setCheckState(Qt::Unchecked); // initialize check state to something valid

KShots
27th July 2007, 18:25
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?

jpn
27th July 2007, 18:58
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. :)

KShots
28th July 2007, 16:29
Thanks, that's exactly the behavior I was looking for :)

chezifresh
19th November 2008, 01:49
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)