Qt-jambi-interest Archive, February 2008
CheckBox delegate in QTableView
Message 1 in thread
Hi people !
I'm trying to implement a delegate to manage boolean data through a
QCheckBox in QTableView instead of normal true/false menu.
To do this I look at spinbox delegate example to write this :
class CheckBoxDelegate extends QItemDelegate {
public CheckBoxDelegate(QWidget parent) {
super(parent);
}
public QWidget createEditor(QWidget parent, QStyleOptionViewItem item,
QModelIndex index) {
QCheckBox editor = new QCheckBox(parent);
return editor;
}
public void setEditorData(QWidget editor, QModelIndex index) {
if (editor instanceof QCheckBox) {
QCheckBox checkBox = (QCheckBox) editor;
checkBox.setChecked(Boolean.parseBoolean(index.model().data(
index, Qt.ItemDataRole.DisplayRole).toString()));
}
}
public void setModelData(QWidget editor, QAbstractItemModel model,
QModelIndex index) {
if (editor instanceof QCheckBox) {
QCheckBox checkBox = (QCheckBox) editor;
boolean value = checkBox.isChecked();
model.setData(index, value, Qt.ItemDataRole.EditRole);
}
}
public void updateEditorGeometry(QWidget editor,
QStyleOptionViewItem item, QModelIndex index) {
editor.setGeometry(item.rect());
}
}
It works as expected, but I would like the checkbox to be displayed all
the time, not only when editing data. In others words my problem is the
checkbox appears only in editing context. In normal display context true
or false strings are still displayed. Furthermore, when entering in
editing mode (double click on the table cell), true or false strings are
displayed **under the checkbox**. How can I do to show only the checkbox ?
Even to simply display model content ?
Do I have to implement paint() method in my delegate class as in
stardelegate example ? It seems to me a bit complicated for displaying a
simple checkbox...
Thanks for any hint...
jMax
Message 2 in thread
moebius@xxxxxxxxxx wrote:
> Hi people !
>
> I'm trying to implement a delegate to manage boolean data through a
> QCheckBox in QTableView instead of normal true/false menu.
>
...
> It works as expected, but I would like the checkbox to be displayed all
> the time, not only when editing data. In others words my problem is the
> checkbox appears only in editing context. In normal display context true
> or false strings are still displayed. Furthermore, when entering in
> editing mode (double click on the table cell), true or false strings are
> displayed **under the checkbox**. How can I do to show only the checkbox ?
> Even to simply display model content ?
How about setIndexWidget()?
http://doc.trolltech.com/qtjambi-4.3.3_01/com/trolltech/qt/gui/QAbstractItemView.html#setIndexWidget(com.trolltech.qt.core.QModelIndex,%20com.trolltech.qt.gui.QWidget)
best regards,
Gunnar