From bce8535b169afdf9cca1390a8f9b79fae11a2952 Mon Sep 17 00:00:00 2001 From: laserpants Date: Mon, 18 Apr 2016 11:36:30 +0300 Subject: [PATCH] implement Checkbox paintEvent --- components/checkbox.cpp | 48 +++++++++++++++++++++++++++++++++++------ components/checkbox.h | 8 +++++-- main.cpp | 2 -- 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/components/checkbox.cpp b/components/checkbox.cpp index e53f870..7fcfab5 100644 --- a/components/checkbox.cpp +++ b/components/checkbox.cpp @@ -1,10 +1,13 @@ #include #include +#include +#include #include "checkbox.h" Checkbox::Checkbox(QWidget *parent) - : QWidget(parent) + : QAbstractButton(parent) { + setFixedSize(200, 200); } Checkbox::~Checkbox() @@ -13,19 +16,52 @@ Checkbox::~Checkbox() void Checkbox::mousePressEvent(QMouseEvent *event) { - QWidget::mousePressEvent(event); + QAbstractButton::mousePressEvent(event); } void Checkbox::mouseReleaseEvent(QMouseEvent *event) { - QWidget::mouseReleaseEvent(event); + QAbstractButton::mouseReleaseEvent(event); } void Checkbox::paintEvent(QPaintEvent *event) { - QPainter painter(this); + Q_UNUSED(event) - painter.drawRect(0, 0, 50, 50); + QStylePainter p(this); + QStyleOptionButton opt; + initStyleOption(&opt); + p.drawControl(QStyle::CE_CheckBox, opt); - QWidget::paintEvent(event); + p.drawRect(rect().adjusted(0, 0, -1, -1)); // tmp +} + +void Checkbox::initStyleOption(QStyleOptionButton *option) const +{ + if (!option) + return; + //Q_D(const QCheckBox); + option->initFrom(this); + /* + if (d->down) + option->state |= QStyle::State_Sunken; + if (d->tristate && d->noChange) + option->state |= QStyle::State_NoChange; + else + option->state |= d->checked ? QStyle::State_On : QStyle::State_Off; + */ + if (testAttribute(Qt::WA_Hover) && underMouse()) { + /* + if (d->hovering) + option->state |= QStyle::State_MouseOver; + else + option->state &= ~QStyle::State_MouseOver; + */ + } + option->text = "Label label"; + /* + option->text = d->text; + option->icon = d->icon; + option->iconSize = iconSize(); + */ } diff --git a/components/checkbox.h b/components/checkbox.h index 540910b..75b6707 100644 --- a/components/checkbox.h +++ b/components/checkbox.h @@ -1,9 +1,11 @@ #ifndef CHECKBOX_H #define CHECKBOX_H -#include +#include -class Checkbox : public QWidget +class QStyleOptionButton; + +class Checkbox : public QAbstractButton { Q_OBJECT @@ -15,6 +17,8 @@ protected: void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + + void initStyleOption(QStyleOptionButton *option) const; }; #endif // CHECKBOX_H diff --git a/main.cpp b/main.cpp index 9d1e773..a793d67 100644 --- a/main.cpp +++ b/main.cpp @@ -6,8 +6,6 @@ int main(int argc, char *argv[]) { QApplication a(argc, argv); - //a.setStyle(new Style); - MainWindow w; w.show();