[Interest] Custom style for QCheckBox in QComboBox

Carel Combrink carel.combrink at gmail.com
Fri Jul 14 16:35:39 CEST 2017


Hi,

How does one customize the style of the QCheckBox inside a QComboBox?

In our application we have a specific style (QSS File) for a QCheckBox, for
example the indicator is a custom image, the font is changed and the colors
are specified.

But this styling does not apply to the QCheckBox that gets created inside a
QComboBox.

What we currently do is create a custom QStyledItemDelegate where we
re-implement the paint() method as follow:

  QStyleOptionButton checkboxstyle;

  checkboxstyle.rect = option.rect;

  const bool data = index.model()->data( index, Qt::CheckStateRole ).toBool();

  checkboxstyle.state  = data ? QStyle::State_On : QStyle::State_Off;

  checkboxstyle.state |= QStyle::State_Enabled;

  checkboxstyle.text   = index.model()->data( QStyle::CE_CheckBox,
Qt::DisplayRole ).toString();


  QCheckBox checkBox;

  QApplication::style()->drawControl( controlStyle, &checkboxstyle,
painter, &checkBox );


This delegate is then set on the QComboBox:

comboBox->setItemDelegate( new CheckableComboBoxDelegate( *this ) );


This works in principle (The QComboBox follows the application style), but
doing this the selection and hovered information is lost compared to the
normal implementation without the delegate set on the QComboBox.

Is this the (only/correct) way to do this or can this also be specified
from the CSS file?

If this is the way, how does one get the correct state information for the
current item so that the delegate can draw the QCheckBox correclty
(different background for the item under the mouse)?

I have tried various methods to initialize and get the
QStyleOptionViewItem::state to reflect this information, but for all
options tried the state stays empty.

If I call the base paint function from my custom paint function the
highlighting works, which leads me to believe that the delegate does have
all information needed, but how do I get access to that information?

Any help in this regard will be appreciated.

PS: This does not work :P
QComboBox QAbstractItemView::QCheckBox{
  background: blue;
}

Regards,
Carel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170714/8c8bd35b/attachment.html>


More information about the Interest mailing list