feat: v1.4.3 成功创建一个自定义的控件对象(还没有对自定义控件进行绘画)

This commit is contained in:
touwoyimuli 2019-12-13 00:12:33 +08:00
parent adeb9cdeb1
commit 97d8b1d9e4
5 changed files with 66 additions and 1 deletions

View File

@ -37,6 +37,11 @@ class ExMyStyle : public QCommonStyle
public: public:
ExMyStyle(); ExMyStyle();
// enum ControlElement {
// CE_SwitchButton = QStyle::CE_CustomBase + 1,
// CE_CustomBase = QStyle::CE_CustomBase + 0xf00000
// };
// QStyle interface // QStyle interface
public: public:
virtual void polish(QWidget *widget) override; virtual void polish(QWidget *widget) override;

View File

@ -80,4 +80,13 @@ void Example::init()
connect(btn, &QPushButton::clicked, this, [=](){ connect(btn, &QPushButton::clicked, this, [=](){
qApp->setStyle(new ExMyStyle()); 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();
} }

View File

@ -22,6 +22,8 @@
#include "ExSwitchButton.h" #include "ExSwitchButton.h"
#include "ExSwitchButton_p.h" #include "ExSwitchButton_p.h"
#include <QDebug> #include <QDebug>
#include <QPainter>
#include "ExMyStyle.h"
EXWIDGET_BEGIN_NAMESPACE EXWIDGET_BEGIN_NAMESPACE
@ -31,7 +33,7 @@ EXWIDGET_BEGIN_NAMESPACE
*/ */
ExSwitchButtonPrivate::ExSwitchButtonPrivate(/*ExSwitchButton *qq*/) ExSwitchButtonPrivate::ExSwitchButtonPrivate(/*ExSwitchButton *qq*/)
{ {
init();
} }
ExSwitchButtonPrivate::~ExSwitchButtonPrivate() ExSwitchButtonPrivate::~ExSwitchButtonPrivate()
@ -51,6 +53,16 @@ void ExSwitchButtonPrivate::init()
q->onChange(true); q->onChange(true);
} }
ExSwitchButton::ExSwitchButton()
{
Q_D(ExSwitchButton);
}
ExSwitchButton::~ExSwitchButton()
{
}
void ExSwitchButton::onChange(bool) void ExSwitchButton::onChange(bool)
{ {
Q_D(ExSwitchButton); Q_D(ExSwitchButton);
@ -60,5 +72,34 @@ void ExSwitchButton::onChange(bool)
qDebug()<<"------01-----ExSwitchButton::onChange(bool):"<<d->m_checked; 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 EXWIDGET_END_NAMESPACE

View File

@ -26,6 +26,8 @@
#include <QtGlobal> #include <QtGlobal>
#include <QAbstractButton> #include <QAbstractButton>
#include <QStyleOption>
//#include <QStyleOptionButton>
EXWIDGET_BEGIN_NAMESPACE EXWIDGET_BEGIN_NAMESPACE
@ -41,6 +43,12 @@ private slots:
private: private:
Q_DECLARE_PRIVATE(ExSwitchButton) Q_DECLARE_PRIVATE(ExSwitchButton)
// QWidget interface
protected:
virtual void paintEvent(QPaintEvent *event) override; //必须重载的纯虚函数
void initStyleOption(QStyleOptionButton* option) const;
}; };
EXWIDGET_END_NAMESPACE EXWIDGET_END_NAMESPACE

View File

@ -43,6 +43,8 @@ public:
explicit ExSwitchButtonPrivate(); explicit ExSwitchButtonPrivate();
~ExSwitchButtonPrivate(); ~ExSwitchButtonPrivate();
// virtual ~QObjectData();
void init(); void init();
public: public: