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-----------------"<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 +#include +#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):"<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 #include +#include +//#include 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: