How to draw checkable checkbox with moved indicator?
-
wrote on 2 Jul 2021, 17:55 last edited by
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?
wrote on 2 Jul 2021, 18:03 last edited by JonB 7 Feb 2021, 18:10@qwe3
This is not my area, but for a checkbox why are you goingQStyleOptionComboBox option;
instead ofQStyleOptionButton
? I thinkQStyle::State_On;
shows the check mark. Which is what I think you are asking about. -
wrote on 2 Jul 2021, 18:11 last edited by qwe3 7 Feb 2021, 18:21
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?Lifetime Qt Championwrote on 3 Jul 2021, 08:12 last edited by mrjj 7 Mar 2021, 08:17Hi
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?
- Also currently I'm not sure it will be aware of being checked.
1/4