implement Checkbox paintEvent

This commit is contained in:
laserpants 2016-04-18 11:36:30 +03:00
parent d2e33cc17e
commit bce8535b16
3 changed files with 48 additions and 10 deletions

View File

@ -1,10 +1,13 @@
#include <QWidget> #include <QWidget>
#include <QPainter> #include <QPainter>
#include <QStylePainter>
#include <QStyleOptionButton>
#include "checkbox.h" #include "checkbox.h"
Checkbox::Checkbox(QWidget *parent) Checkbox::Checkbox(QWidget *parent)
: QWidget(parent) : QAbstractButton(parent)
{ {
setFixedSize(200, 200);
} }
Checkbox::~Checkbox() Checkbox::~Checkbox()
@ -13,19 +16,52 @@ Checkbox::~Checkbox()
void Checkbox::mousePressEvent(QMouseEvent *event) void Checkbox::mousePressEvent(QMouseEvent *event)
{ {
QWidget::mousePressEvent(event); QAbstractButton::mousePressEvent(event);
} }
void Checkbox::mouseReleaseEvent(QMouseEvent *event) void Checkbox::mouseReleaseEvent(QMouseEvent *event)
{ {
QWidget::mouseReleaseEvent(event); QAbstractButton::mouseReleaseEvent(event);
} }
void Checkbox::paintEvent(QPaintEvent *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();
*/
} }

View File

@ -1,9 +1,11 @@
#ifndef CHECKBOX_H #ifndef CHECKBOX_H
#define CHECKBOX_H #define CHECKBOX_H
#include <QWidget> #include <QAbstractButton>
class Checkbox : public QWidget class QStyleOptionButton;
class Checkbox : public QAbstractButton
{ {
Q_OBJECT Q_OBJECT
@ -15,6 +17,8 @@ protected:
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void initStyleOption(QStyleOptionButton *option) const;
}; };
#endif // CHECKBOX_H #endif // CHECKBOX_H

View File

@ -6,8 +6,6 @@ int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
//a.setStyle(new Style);
MainWindow w; MainWindow w;
w.show(); w.show();