PDA

View Full Version : Changing QStyleOptionButton (QCheckbox) to use StyleSheet not working????



jonathan_MVE
10th July 2015, 17:09
HI,

I am trying to use a style sheet to change a QStyleOptionButton to work as a QCheckBox but display with my own checked/unchecked indicators. I have had success setting the stylesheet on normal checkboxes but in this case I need to use a QStyleOptionButton and this doesnt seem to work? My code is as follows:



QStyleOptionButton option;
//m_checkBoxRect is previously setup
m_checkboxRect.setY(rect.height() / 2 - m_checkboxRect.height() / 2);

QCheckBox* styledCheckBox = new QCheckBox();
styledCheckBox->setGeometry(m_checkboxRect);
styledCheckBox->setCheckState(m_checkboxState);
styledCheckBox->setStyleSheet(
"QCheckedBox::indicator:unchecked { image: url(:/Resources/indicator_off.png);}"
"QCheckedBox::indicator:checked { image: url(:/Resources/indicator_on.png);}"
"QCheckedBox::indicator:indeterminate { image: url(:/Resources/indicator_partial.png);}"
);

option.initFrom(styledCheckBox);
option.rect = m_checkboxRect;

style()->drawControl(QStyle::CE_CheckBox, &option, painter, this);



All I seem to get back is an unchecked checkbox which doesn't respond to my mouse presses.

Does anyone have any ideas as to why this wont work, or perhaps another route??

jonathan_MVE
14th July 2015, 10:22
I managed to find a solution to this.

By simply drawing the pixmap rather than the control element I am able to produce th desired results. The mouse press event for the parent widget can allow me to detect mouse clicks over the m_checkboxRect area and therefore maintain the checked state.



switch (m_checkboxState)
{
case Qt::Checked:
option.state = QStyle::State_On;
indicatorPixmap = QPixmap(":/Resources/indicator_on.png");
break;
case Qt::Unchecked:
option.state = QStyle::State_Off;
indicatorPixmap = QPixmap(":/Resources/indicator_off.png");
break;
case Qt::PartiallyChecked:
option.state = QStyle::State_NoChange;
indicatorPixmap = QPixmap(":/Resources/indicator_partial.png");
break;
default:
break;
}

style()->drawItemPixmap(painter, m_checkboxRect, 0, indicatorPixmap );