feat: The custom control MySwitchButton has been implemented
This commit is contained in:
@@ -21,7 +21,6 @@
|
||||
|
||||
#include "mystyle.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
|
||||
@@ -38,7 +37,6 @@ void MyStyle::drawPrimitive(const QStyle *style, MyStyle::PrimitiveElement pe, c
|
||||
|
||||
void MyStyle::drawControl(const QStyle *style, MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QRect MyStyle::subElementRect(const QStyle *style, MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget)
|
||||
@@ -102,19 +100,37 @@ void MyStyle::unpolish(QWidget *widget)
|
||||
QCommonStyle::unpolish(widget);
|
||||
}
|
||||
|
||||
|
||||
void MyStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
|
||||
{
|
||||
switch (pe) {
|
||||
case PE_SwitchButtonGroove: {
|
||||
if (const QStyleOptionButton* swtchBtn = qstyleoption_cast<const QStyleOptionButton*>(opt)) {
|
||||
p->setBrush(Qt::red);
|
||||
p->drawRoundedRect(swtchBtn->rect, 8, 8);
|
||||
if (const QStyleOptionButton* switchBtn = qstyleoption_cast<const QStyleOptionButton*>(opt)) {
|
||||
p->setPen(Qt::NoPen);
|
||||
if (switchBtn->state & State_On)
|
||||
p->setBrush(QColor("#77d472"));
|
||||
|
||||
if (switchBtn->state & State_Off) {
|
||||
p->setPen(QPen(QColor("#e5e5e5"), 2));
|
||||
p->setBrush(QColor("#fdfefd"));
|
||||
}
|
||||
|
||||
p->drawRoundedRect(switchBtn->rect.adjusted(1, 1, -1, -1), switchBtn->rect.height() / 2.0, switchBtn->rect.height() / 2.0);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case PE_SwitchButtonHandle: {
|
||||
if (const QStyleOptionButton* switchBtn = qstyleoption_cast<const QStyleOptionButton*>(opt)) {
|
||||
p->setPen(Qt::NoPen);
|
||||
if (switchBtn->state & State_On)
|
||||
p->setBrush(QColor("#fefffe"));
|
||||
|
||||
if (switchBtn->state & State_Off)
|
||||
p->setPen(QPen(QColor("#e5e5e5"), 2));
|
||||
|
||||
QRect rectHandle = switchBtn->rect.adjusted(1, 1, -1, -1);
|
||||
int r = qMin(rectHandle.width() / 2.0, rectHandle.height() / 2.0);
|
||||
p->drawEllipse(rectHandle.center(), r, r);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@@ -185,7 +201,6 @@ QRect MyStyle::subElementRect(QStyle::SubElement subElement, const QStyleOption
|
||||
case SE_SwitchButtonHandle: {
|
||||
if (const QStyleOptionButton* switchBtn = qstyleoption_cast<const QStyleOptionButton*>(option)) {
|
||||
int handleWidth = pixelMetric(PM_SwitchButtonHandleWidth, option, widget);
|
||||
//pixelMetric(PM_SwitchButtonHandleWidth, option, widget);
|
||||
QRect rectHandle(0, 0, 0, 0);
|
||||
rectHandle.setHeight(switchBtn->rect.height());
|
||||
|
||||
@@ -194,10 +209,10 @@ QRect MyStyle::subElementRect(QStyle::SubElement subElement, const QStyleOption
|
||||
else
|
||||
rectHandle.setWidth(handleWidth);
|
||||
|
||||
if (switchBtn->state & QStyle::State_Off)
|
||||
rectHandle.moveLeft(switchBtn->rect.left() + 5);
|
||||
if (switchBtn->state & QStyle::State_On)
|
||||
rectHandle.moveRight(switchBtn->rect.right());
|
||||
else
|
||||
rectHandle.moveLeft(switchBtn->rect.left());
|
||||
rectHandle.moveRight(switchBtn->rect.right() - 5);
|
||||
|
||||
return rectHandle;
|
||||
}
|
||||
@@ -296,6 +311,21 @@ void MyStylePainter::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOpti
|
||||
m_qStyle->drawPrimitive(pe, opt, this, m_widget);
|
||||
}
|
||||
|
||||
MyStylePainter::MyStylePainter()
|
||||
: QPainter()
|
||||
{
|
||||
m_widget = nullptr;
|
||||
m_myStyleHelp = nullptr;
|
||||
}
|
||||
|
||||
MyStylePainter::MyStylePainter(QWidget* w)
|
||||
{
|
||||
m_widget = w;
|
||||
m_qStyle = w->style();
|
||||
m_myStyleHelp.setStyle(m_qStyle);
|
||||
QPainter::begin(w); //是调用父类的 begin(), 调试半天才发现
|
||||
}
|
||||
|
||||
void MyStylePainter::drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt)
|
||||
{
|
||||
m_myStyleHelp.drawPrimitive(pe, opt, this, m_widget);
|
||||
@@ -310,4 +340,3 @@ void MyStylePainter::drawControl(MyStyle::ControlElement element, const QStyleOp
|
||||
{
|
||||
m_myStyleHelp.drawControl(element, opt, this, m_widget);
|
||||
}
|
||||
|
||||
|
@@ -106,7 +106,6 @@ public:
|
||||
using QCommonStyle::pixelMetric;
|
||||
using QCommonStyle::sizeFromContents;
|
||||
using QCommonStyle::styleHint;
|
||||
|
||||
};
|
||||
|
||||
class MyStyleHelp
|
||||
@@ -132,13 +131,14 @@ private:
|
||||
class MyStylePainter : public QPainter
|
||||
{
|
||||
public:
|
||||
MyStylePainter() {}
|
||||
MyStylePainter();
|
||||
MyStylePainter(QWidget* w);
|
||||
~MyStylePainter() {}
|
||||
|
||||
inline void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt);
|
||||
inline void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt);
|
||||
inline void drawControl(MyStyle::ControlElement element, const QStyleOption *opt);
|
||||
inline void drawControl(QStyle::ControlElement element, const QStyleOption *opt);
|
||||
/*inline*/ void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt);
|
||||
/*inline*/ void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt);
|
||||
/*inline*/ void drawControl(MyStyle::ControlElement element, const QStyleOption *opt);
|
||||
/*inline*/ void drawControl(QStyle::ControlElement element, const QStyleOption *opt);
|
||||
|
||||
private:
|
||||
QWidget* m_widget;
|
||||
|
@@ -4,6 +4,9 @@
|
||||
|
||||
#include <QStylePainter>
|
||||
#include <QStyleOption>
|
||||
#include <QDebug>
|
||||
|
||||
class MyStylePainter;
|
||||
|
||||
MySwitchButton::MySwitchButton(QWidget *parent)
|
||||
: QAbstractButton(parent)
|
||||
@@ -20,7 +23,7 @@ MySwitchButton::~MySwitchButton()
|
||||
|
||||
QSize MySwitchButton::sizeHint() const
|
||||
{
|
||||
return QSize(100, 40);
|
||||
return QSize(60, 40);
|
||||
}
|
||||
|
||||
void MySwitchButton::paintEvent(QPaintEvent *event)
|
||||
@@ -30,11 +33,9 @@ void MySwitchButton::paintEvent(QPaintEvent *event)
|
||||
QStyleOptionButton opt;
|
||||
initStyleOption(&opt);
|
||||
|
||||
// MyStylePainter painter(this);
|
||||
// MyStylePainter painter;
|
||||
// painter.drawControl(MyStyle::CE_SwitchButton, &opt);//static_cast<QStyleOption>(opt));
|
||||
|
||||
// pa.drawControl(MyStyle::CE_SwitchButton, opt);
|
||||
MyStylePainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
painter.drawControl(MyStyle::CE_SwitchButton, &opt);
|
||||
}
|
||||
|
||||
void MySwitchButton::initStyleOption(QStyleOptionButton *opt) const
|
||||
@@ -57,16 +58,20 @@ MySwitchButtonPrivate::MySwitchButtonPrivate()
|
||||
|
||||
MySwitchButtonPrivate::~MySwitchButtonPrivate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MySwitchButtonPrivate::init()
|
||||
{
|
||||
Q_Q(MySwitchButton);
|
||||
|
||||
checked = false;
|
||||
animationStart = 0;
|
||||
animationEnd = 1;
|
||||
check = false;
|
||||
q->setObjectName("MySwitchButton");
|
||||
// q->connect(q, &MySwitchButton::toggled, q, &MySwitchButton::checkedChanged);
|
||||
q->setChecked(true);
|
||||
q->setCheckable(true); //clicked toggled douxuyaokauqi
|
||||
q->connect(q, SIGNAL(clicked(bool)), q, SLOT(setChecked(bool)));
|
||||
}
|
||||
|
||||
bool MySwitchButtonPrivate::switchCheck()
|
||||
{
|
||||
return check;
|
||||
}
|
||||
|
@@ -1,3 +1,24 @@
|
||||
/*
|
||||
*
|
||||
* Gmail: touwoyimuli@gmai.com
|
||||
* blogs: https://blog.csdn.net/qq_33154343
|
||||
*
|
||||
* -------------------------------------------------
|
||||
* Copyright 2019~2020 touwoyimuli
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MYSWITCHBUTTON_H
|
||||
#define MYSWITCHBUTTON_H
|
||||
|
||||
@@ -23,7 +44,6 @@ protected:
|
||||
private:
|
||||
void initStyleOption(QStyleOptionButton *opt) const;
|
||||
|
||||
private:
|
||||
Q_DECLARE_PRIVATE(MySwitchButton)
|
||||
};
|
||||
|
||||
|
@@ -1,3 +1,24 @@
|
||||
/*
|
||||
*
|
||||
* Gmail: touwoyimuli@gmai.com
|
||||
* blogs: https://blog.csdn.net/qq_33154343
|
||||
*
|
||||
* -------------------------------------------------
|
||||
* Copyright 2019~2020 touwoyimuli
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef MYSWITCHBUTTON_P_H
|
||||
#define MYSWITCHBUTTON_P_H
|
||||
|
||||
@@ -16,11 +37,11 @@ public:
|
||||
~MySwitchButtonPrivate();
|
||||
|
||||
void init();
|
||||
bool setSwitchCheck(bool check);
|
||||
bool switchCheck();
|
||||
|
||||
public:
|
||||
bool checked;
|
||||
double animationStart;
|
||||
double animationEnd;
|
||||
private:
|
||||
bool check;
|
||||
|
||||
public:
|
||||
Q_DECLARE_PUBLIC(MySwitchButton) //要加上宏
|
||||
|
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "widget.h"
|
||||
#include "mystyle.h"
|
||||
#include "myswitchbutton.h"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QTableWidget>
|
||||
@@ -75,6 +76,11 @@ void Widget::init()
|
||||
progreV->setValue(67);
|
||||
progreV->setOrientation(Qt::Vertical);
|
||||
|
||||
//自定义的控件
|
||||
MySwitchButton* switchBtn = new MySwitchButton(this);
|
||||
switchBtn->resize(80, 40);
|
||||
switchBtn->move(200 ,300);
|
||||
|
||||
int i = 0;
|
||||
QStringList listStyle = QStyleFactory::keys();
|
||||
|
||||
@@ -95,4 +101,5 @@ void Widget::init()
|
||||
qApp->setStyle(new MyStyle());
|
||||
});
|
||||
|
||||
qApp->setStyle(new MyStyle());
|
||||
}
|
||||
|
Reference in New Issue
Block a user