PDA

View Full Version : QTreeWidgetItem + radio Button



desch
14th November 2007, 11:40
Hi Guys,

Do you know a way to make QtreeWidgetItem with radio button inside instead of checkboxes?

Thanks,

David

wysota
14th November 2007, 12:35
Do you want those buttons to be mutually exclusive?

desch
14th November 2007, 13:07
Yes ; that's it and Radio Button is exactly done for that; but can't set it in QTreeWidgetItem

So thanks for your help
Dzienkuje:p


David

wysota
14th November 2007, 13:14
I know what radio buttons are for :) Ok, you have to implement the feature yourself. First thing is to provide a delegate to draw the radio button instead of a checkbox - you can do that by reimplementing drawCheck in QItemDelegate. Next you have to make sure that only one item is checked at a time. To do that you need to connect to the models dataChanged() signal and look for changes in the check state. If you notice that an item gets checked, you need to uncheck all others. It'd be easier to do with the model-view approach and a custom model, but if you need that for QTreeWidget, you'll need to write a bit more complex code.

desch
14th November 2007, 13:23
OK,

Thanks for your quick reply

mchara
14th November 2007, 15:15
Hi,
Another approach is QTreeWidget->setItemWidget(), if you don't want to deal with itemDelegates, that are rather meant for view-model aproach, however you still have to make sure that only one item is checked, as wysota said, except you would have to connect QRadioButton's signals instead of dataModels one.
You can provide i.e. QMap<QTreeWidgetItem*, QRadioButton*>, QList<QRadioButton*> or something similar filled on setItemWidget calls to know what you'll need to uncheck.
sender() method can be usefull to determine which radioButton should remain checked.