From 97d8b1d9e4668a692c2f1316c5ed39cc6dfe3437 Mon Sep 17 00:00:00 2001 From: touwoyimuli <touwoyimuli@gmail.com> Date: Fri, 13 Dec 2019 00:12:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20v1.4.3=20=E6=88=90=E5=8A=9F=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E4=B8=80=E4=B8=AA=E8=87=AA=E5=AE=9A=E4=B9=89=E7=9A=84?= =?UTF-8?q?=E6=8E=A7=E4=BB=B6=E5=AF=B9=E8=B1=A1=EF=BC=88=E8=BF=98=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E5=AF=B9=E8=87=AA=E5=AE=9A=E4=B9=89=E6=8E=A7=E4=BB=B6?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E7=BB=98=E7=94=BB=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtCustomStyleEx/ExMyStyle.h | 5 +++ QtCustomStyleEx/Example.cpp | 9 +++++ QtCustomStyleEx/MyWidget/ExSwitchButton.cpp | 43 ++++++++++++++++++++- QtCustomStyleEx/MyWidget/ExSwitchButton.h | 8 ++++ QtCustomStyleEx/MyWidget/ExSwitchButton_p.h | 2 + 5 files changed, 66 insertions(+), 1 deletion(-) diff --git a/QtCustomStyleEx/ExMyStyle.h b/QtCustomStyleEx/ExMyStyle.h index 162f1b4..fb4a1e6 100644 --- a/QtCustomStyleEx/ExMyStyle.h +++ b/QtCustomStyleEx/ExMyStyle.h @@ -37,6 +37,11 @@ class ExMyStyle : public QCommonStyle public: ExMyStyle(); +// enum ControlElement { +// CE_SwitchButton = QStyle::CE_CustomBase + 1, +// CE_CustomBase = QStyle::CE_CustomBase + 0xf00000 +// }; + // QStyle interface public: virtual void polish(QWidget *widget) override; diff --git a/QtCustomStyleEx/Example.cpp b/QtCustomStyleEx/Example.cpp index bc8a47a..08ffafd 100644 --- a/QtCustomStyleEx/Example.cpp +++ b/QtCustomStyleEx/Example.cpp @@ -80,4 +80,13 @@ void Example::init() connect(btn, &QPushButton::clicked, this, [=](){ qApp->setStyle(new ExMyStyle()); }); + + ExSwitchButton* switchBtn = new ExSwitchButton(); + switchBtn->move(300, 300); +// switchBtn->move(700, 500); + switchBtn->resize(200, 200); + switchBtn->setText("switchBtn"); + switchBtn->setParent(this); + switchBtn->show(); + qDebug()<<"----03-----------------"<<switchBtn->rect(); } diff --git a/QtCustomStyleEx/MyWidget/ExSwitchButton.cpp b/QtCustomStyleEx/MyWidget/ExSwitchButton.cpp index fa8bfb8..56f0b93 100644 --- a/QtCustomStyleEx/MyWidget/ExSwitchButton.cpp +++ b/QtCustomStyleEx/MyWidget/ExSwitchButton.cpp @@ -22,6 +22,8 @@ #include "ExSwitchButton.h" #include "ExSwitchButton_p.h" #include <QDebug> +#include <QPainter> +#include "ExMyStyle.h" EXWIDGET_BEGIN_NAMESPACE @@ -31,7 +33,7 @@ EXWIDGET_BEGIN_NAMESPACE */ ExSwitchButtonPrivate::ExSwitchButtonPrivate(/*ExSwitchButton *qq*/) { - + init(); } ExSwitchButtonPrivate::~ExSwitchButtonPrivate() @@ -51,6 +53,16 @@ void ExSwitchButtonPrivate::init() q->onChange(true); } +ExSwitchButton::ExSwitchButton() +{ + Q_D(ExSwitchButton); +} + +ExSwitchButton::~ExSwitchButton() +{ + +} + void ExSwitchButton::onChange(bool) { Q_D(ExSwitchButton); @@ -60,5 +72,34 @@ void ExSwitchButton::onChange(bool) qDebug()<<"------01-----ExSwitchButton::onChange(bool):"<<d->m_checked; } +void ExSwitchButton::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event); + + QPainter painter(this); + QStyleOptionButton opt; + initStyleOption(&opt); + + ExMyStyle MyStyle; +// MyStyle.drawControl(ExMyStyle::CE_Sw); + + +} + +void ExSwitchButton::initStyleOption(QStyleOptionButton *option) const +{ + if (!option) + return; + + option->init(this); //QStyleOption 对象进行初始化(其中属性很多,整体初始化,然后局部进行参数修改) + option->initFrom(this); + + if (isChecked()) { + option->state |= QStyle::State_On; + } else { + option->state |= QStyle::State_Off; + } +} + EXWIDGET_END_NAMESPACE diff --git a/QtCustomStyleEx/MyWidget/ExSwitchButton.h b/QtCustomStyleEx/MyWidget/ExSwitchButton.h index 7606fc1..0da3126 100644 --- a/QtCustomStyleEx/MyWidget/ExSwitchButton.h +++ b/QtCustomStyleEx/MyWidget/ExSwitchButton.h @@ -26,6 +26,8 @@ #include <QtGlobal> #include <QAbstractButton> +#include <QStyleOption> +//#include <QStyleOptionButton> EXWIDGET_BEGIN_NAMESPACE @@ -41,6 +43,12 @@ private slots: private: Q_DECLARE_PRIVATE(ExSwitchButton) + + // QWidget interface +protected: + virtual void paintEvent(QPaintEvent *event) override; //必须重载的纯虚函数 + void initStyleOption(QStyleOptionButton* option) const; + }; EXWIDGET_END_NAMESPACE diff --git a/QtCustomStyleEx/MyWidget/ExSwitchButton_p.h b/QtCustomStyleEx/MyWidget/ExSwitchButton_p.h index 36b8aad..b60251d 100644 --- a/QtCustomStyleEx/MyWidget/ExSwitchButton_p.h +++ b/QtCustomStyleEx/MyWidget/ExSwitchButton_p.h @@ -43,6 +43,8 @@ public: explicit ExSwitchButtonPrivate(); ~ExSwitchButtonPrivate(); +// virtual ~QObjectData(); + void init(); public: