custom qstyleditemdelegate (checkbox) displays number instead of checkbox
-
Hi, [set to solved, although the delegate does not work, but concrete question is answered.]
in order to paint the surrounding rectangle of the checkbox in different colours according to some value, I had to subclass QStyledItemDelegate. At first glance, it seems to work, but when I try to check or uncheck the box a number is displayed instead of the box, I've just checked. (Actually there remains a unchanged checkbox in the center of the cell.) [It is integrated in an tableview.]
The number seems to be the Qt::CheckState constant. But how could I debug this strange behaviour?
Kind regards,
Andreas
-
@JonB Yes, I'm also suspecting the paint method. I copied some code from an example somewhere....
auto stateFlag = opt.checkState == Qt::Checked ? QStyle::State_On : QStyle::State_Off; opt.state.setFlag(stateFlag, true);
Maybe it's this snippet, which causes the trouble.
-
@JonB Ok, here are the relevant parts of the paint method:
void CheckBoxZBuchDeckDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt = option; //some code to colorize the background... initStyleOption(&opt, index); auto * widget = opt.widget; auto * style = widget->style(); int zeile = index.row(); if ((style) && (zeile < index.model()->rowCount() - 1)) { opt.state.setFlag(QStyle::State_HasFocus, false); opt.features.setFlag(QStyleOptionViewItem::HasDisplay, false); opt.features.setFlag(QStyleOptionViewItem::HasDecoration, false); opt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false); style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget); qDebug() << "Checkbox delegate paint method called"; opt.rect = this->getCheckBoxRect(opt); auto stateFlag = opt.checkState == Qt::Checked ? QStyle::State_On : QStyle::State_Off; opt.state.setFlag(stateFlag, true); style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget); } else { painter->setPen(Qt::black); int spalte = index.column(); int sum_Flags = this->get_intFromVariant(index.siblingAtColumn(spalte).data(Qt::DisplayRole)); QString cell_text = QString::number(sum_Flags); painter->drawText(opt.rect, Qt::AlignCenter, cell_text); } }
The last line shows the sum of all flags set by the checkbox...
-
@andi456 said in custom qstyleditemdelegate (checkbox) displays number instead of checkbox:
if ((style) && (zeile < index.model()->rowCount() - 1)) {
This should be
zeile <= index.model()->rowCount() - 1
orzeile < index.model()->rowCount()
. With your code whenever theindex
passed in is the last one (index.model()->rowCount() - 1
) you go into theelse
and paint the number, hence the behaviour you see?This is why it's always good to show code!
-
@andi456
My (next) guess: you haveopt.features.setFlag(QStyleOptionViewItem::HasCheckIndicator, false); ... style->drawPrimitive(QStyle::PE_IndicatorViewItemCheck, &opt, painter, widget);
[BTW, that should be
QStyle::PE_IndicatorItemViewItemCheck
now.]
So you ask it to show the checkstate value but have no check indicator, maybe that shows it is a number?Maybe you need to restore that flag before painting the checkbox, or take a look at, say, https://forum.qt.io/topic/4277/solved-painting-in-custom-delegate where the
option
is copied to amodifiedOption
for the checkbox? -
@JonB Tried to implement the hints given in the post you linked. It did center the checkBoxes shown initially, but the checkBox shown, if I try to change the data, is being drawn to the left of the initial checkBox.
Does one need to adjust the createEditor function too?
-