How to draw checkable checkbox with moved indicator?
-
Hi,
I would like to draw checkable checkbox with indicator in position (0, heightOfCheckBox()/2 - heightOfIndicator()/2 ).
I have a class:
#include "mywidget.h" #include <QStyle> #include <QStyleOptionComboBox> myWidget::myWidget(QWidget *parent):QCheckBox(parent) { setFixedSize(200,50); setCheckable(true); } void myWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.fillRect(rect(), Qt::red); QStyleOptionComboBox option; option.initFrom(this); option.rect = QRect(0,rect().height()/2-5,10,10); style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, &painter, this); } bool myWidget::hitButton(const QPoint &pos) const { return rect().contains(pos); }
And I can draw that moved indicator, but when I clicked in checkBox, it doesn't change his state. What is a proper solution?
-
Hi,
I would like to draw checkable checkbox with indicator in position (0, heightOfCheckBox()/2 - heightOfIndicator()/2 ).
I have a class:
#include "mywidget.h" #include <QStyle> #include <QStyleOptionComboBox> myWidget::myWidget(QWidget *parent):QCheckBox(parent) { setFixedSize(200,50); setCheckable(true); } void myWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); painter.fillRect(rect(), Qt::red); QStyleOptionComboBox option; option.initFrom(this); option.rect = QRect(0,rect().height()/2-5,10,10); style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, &painter, this); } bool myWidget::hitButton(const QPoint &pos) const { return rect().contains(pos); }
And I can draw that moved indicator, but when I clicked in checkBox, it doesn't change his state. What is a proper solution?
-
I have other problem too: when I change numbers in option.rect the indicator change position ( x is not in 0 ):
option.rect = QRect(0,rect().height()/2-10,20,20);
Yeah, I think
QStyleOptionButton
is better. But I would like to automatic change check mark when I click on checkBox. But maybe I have to reimplement mousePressEvent and call repaint() and in paintEvent() have if-statement to change check mark? -
I have other problem too: when I change numbers in option.rect the indicator change position ( x is not in 0 ):
option.rect = QRect(0,rect().height()/2-10,20,20);
Yeah, I think
QStyleOptionButton
is better. But I would like to automatic change check mark when I click on checkBox. But maybe I have to reimplement mousePressEvent and call repaint() and in paintEvent() have if-statement to change check mark?Hi
The normal paintEvent is likevoid QCheckBox::paintEvent(QPaintEvent *) { QStylePainter p(this); QStyleOptionButton opt; initStyleOption(&opt); p.drawControl(QStyle::CE_CheckBox, opt); } and drawControl uses subopt.rect = subElementRect(isRadio ? SE_RadioButtonIndicator : SE_CheckBoxIndicator, btn, widget);
to get the location of the "hot area" where you can click it. so if you change the option.rect, it will not work as expected as it's not the whole area that is used for hit testing.
4- Also currently I'm not sure it will be aware of being checked.
Try adding
option.state |= QStyle::State_On; and see.
not sure it works for QStyleOptionComboBox as normally one uses QStyleOptionButton.
https://code.woboq.org/qt5/qtbase/src/widgets/styles/qcommonstyle.cpp.html#1313
- But I'm a bit confused about the actual goal since you inherited a CheckBox.
Do you want a special button-like widget were once checked, it will show the checkmark in some position you want. Or should be like a normal QCheckBox and you just want to center the Checkmark?
Im asking as QCheckBox already center on y ?
- Also currently I'm not sure it will be aware of being checked.