feat: The custom control MySwitchButton has been implemented
This commit is contained in:
parent
7548546060
commit
17dc1f5d42
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#ifndef EXMYGLOBAL_H
|
||||
#define EXMYGLOBAL_H
|
||||
|
||||
//定义命名空间宏
|
||||
#define EXWIDGET_BEGIN_NAMESPACE namespace ExPlumStyle {
|
||||
#define EXWIDGET_END_NAMESPACE }
|
||||
|
||||
#define EXWIDGET_USE_NAMESPACE using namespace ExPlumStyle;
|
||||
|
||||
//ExPlumStyle命名空间里面嵌套的的子命名空间
|
||||
#define EXDRAWUTILS_BEGIN_NAMESPACE namespace ExDrawUtils {
|
||||
#define EXDRAWUTILS_END_NAMESPACE }
|
||||
|
||||
#endif // EXMYGLOBAL_H
|
@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#include "ExMyStyle.h"
|
||||
|
||||
#include <QProxyStyle>
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
#include <QLinearGradient>
|
||||
|
||||
EXWIDGET_BEGIN_NAMESPACE
|
||||
ExMyStyle::ExMyStyle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExMyStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ExMyStyle::polish(QWidget *widget)
|
||||
{
|
||||
QCommonStyle::polish(widget);
|
||||
}
|
||||
|
||||
void ExMyStyle::unpolish(QWidget *widget)
|
||||
{
|
||||
QCommonStyle::unpolish(widget);
|
||||
}
|
||||
|
||||
void ExMyStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const
|
||||
{
|
||||
p->setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
switch (element) {
|
||||
case CE_ScrollBarAddPage: { //增加页(在滑槽)
|
||||
p->fillRect(opt->rect, QColor("#bfe9ff"));
|
||||
return;
|
||||
}
|
||||
case CE_ScrollBarSubPage: { //减少页(在滑槽)
|
||||
p->fillRect(opt->rect, QColor("#EC6EAD"));
|
||||
return;
|
||||
}
|
||||
case CE_ScrollBarSlider: { //滑块
|
||||
p->fillRect(opt->rect, QColor("#A8BFFF"));
|
||||
return;
|
||||
}
|
||||
case CE_ScrollBarAddLine: { //增加按钮
|
||||
p->fillRect(opt->rect, QColor("#21d4fd"));
|
||||
return;
|
||||
}
|
||||
case CE_ScrollBarSubLine: { //减少按钮
|
||||
p->fillRect(opt->rect, QColor("#de6161"));
|
||||
return;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
QCommonStyle::drawControl(element, opt, p, w);
|
||||
}
|
||||
|
||||
void ExMyStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const
|
||||
{
|
||||
switch (cc) {
|
||||
// case CC_:
|
||||
// break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
QCommonStyle::drawComplexControl(cc, opt, p, widget);
|
||||
}
|
||||
|
||||
QSize ExMyStyle::sizeFromContents(QStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const
|
||||
{
|
||||
QSize size = QCommonStyle::sizeFromContents(ct, opt, contentsSize, w);
|
||||
|
||||
switch (ct) {
|
||||
case CT_ScrollBar: {
|
||||
int widget = size.width();
|
||||
int height = size.height();
|
||||
|
||||
if (widget < height)
|
||||
size.setHeight(height * 1.2);
|
||||
|
||||
if (widget > height)
|
||||
size.setWidth(widget * 1.2);
|
||||
|
||||
qDebug()<<"----01----:"<<size;
|
||||
return size;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
QRect ExMyStyle::subElementRect(QStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const
|
||||
{
|
||||
switch (subElement) {
|
||||
// case SE_:
|
||||
// break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QCommonStyle::subElementRect(subElement, option, widget);
|
||||
}
|
||||
|
||||
int ExMyStyle::pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
|
||||
{
|
||||
switch (metric) {
|
||||
// case PM_:
|
||||
// return 36;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QCommonStyle::pixelMetric(metric, option, widget);
|
||||
}
|
||||
|
||||
int ExMyStyle::styleHint(QStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const
|
||||
{
|
||||
switch (stylehint) {
|
||||
// case SH_:
|
||||
// break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return QCommonStyle::styleHint(stylehint, opt, widget, returnData);
|
||||
}
|
||||
|
||||
EXWIDGET_END_NAMESPACE
|
@ -1,153 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#ifndef EXMYSTYLE_H
|
||||
#define EXMYSTYLE_H
|
||||
|
||||
#include "ExMyGlobal.h"
|
||||
|
||||
#include "qglobal.h"
|
||||
#include <QCommonStyle>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
//class QCommonStyle;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
EXWIDGET_BEGIN_NAMESPACE
|
||||
class ExMyStyle : public QCommonStyle
|
||||
{
|
||||
// Q_OBJECT
|
||||
|
||||
public:
|
||||
ExMyStyle();
|
||||
|
||||
// enum ControlElement {
|
||||
// CE_SwitchButton = QStyle::CE_CustomBase + 1,
|
||||
// CE_CustomBase = QStyle::CE_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
public:
|
||||
// enum PrimitiveElement {
|
||||
// PE_ItemBackground = QStyle::PE_CustomBase + 1, //列表项的背景色
|
||||
// PE_SwitchButtonGroove,
|
||||
// PE_SwitchButtonHandle,
|
||||
// PE_CustomBase = QStyle::PE_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// enum ControlElement {
|
||||
// CE_IconButton = QStyle::CE_CustomBase + 1,
|
||||
// CE_SwitchButton,
|
||||
// CE_CustomBase = QStyle::CE_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// enum SubElement {
|
||||
// SE_IconButtonIcon = QStyle::SE_CustomBase + 1,
|
||||
// SE_SwitchButtonGroove,
|
||||
// SE_SwitchButtonHandle,
|
||||
// SE_CustomBase = QStyle::SE_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// enum PixelMetric {
|
||||
// PM_FocusBorderWidth = QStyle::PM_CustomBase + 1, //控件焦点状态的边框宽度
|
||||
// PM_FrameRadius,
|
||||
// PM_CustomBase = QStyle::PM_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// enum ContentsType {
|
||||
// CT_IconButton = QStyle::CT_CustomBase + 1,
|
||||
// CT_SwitchButton,
|
||||
// CT_CustomBase = QStyle::CT_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// enum StyleHint {
|
||||
// SH_CustomBase = QStyle::SH_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// enum StandardPixmap {
|
||||
// SP_CustomBase = QStyle::SP_CustomBase + 0xf00000
|
||||
// };
|
||||
|
||||
// /*枚举通过按位 ~ ^ | !来计算得到需要组合
|
||||
// * //0b对应的二进制 0x十六进制 对应的十进制
|
||||
// * 0b0000 0001 0x0000 0001 1
|
||||
// * 0b0000 0010 0x0000 0002 2
|
||||
// * 0b0000 0100 0x0000 0004 4
|
||||
// * 0b0000 1000 0x0000 0006 8
|
||||
// * 0b0001 0000 0x0000 0010 16
|
||||
// */
|
||||
|
||||
// enum StyleState {
|
||||
// SS_NormalState = 0x00000000,
|
||||
// SS_HoverState = 0x00000001,
|
||||
// SS_PressState = 0x00000002,
|
||||
// };
|
||||
// Q_DECLARE_FLAGS(StateFlags, StyleState) //而类型定义(typedef)和操作符重载正是宏Q_DECLARE_FLAGS 和 Q_DECLARE_OPERATORS_FOR_FLAGS (均无关于元对象系统)
|
||||
|
||||
//静态static函数
|
||||
// static void drawPrimitive(const QStyle *style, ExCustomStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w);
|
||||
// static void drawControl(const QStyle *style, ExCustomStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w);
|
||||
// static int pixelMetric(const QStyle *style, QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget);
|
||||
// static QRect subElementRect(const QStyle *style, ExCustomStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget);
|
||||
// static QSize sizeFromContents(const QStyle *style, ExCustomStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w);
|
||||
// static QIcon standardIcon(const QStyle *style, ExCustomStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget);
|
||||
|
||||
|
||||
//内联inline函数,然后[自动选择]是调用静态函数,还是重写的基类的函数
|
||||
inline void drawPrimitive(ExMyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const;
|
||||
// inline void drawControl(ExCustomStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const;
|
||||
// inline QRect subElementRect(ExCustomStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const;
|
||||
// inline int pixelMetric(ExCustomStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const;
|
||||
// inline QSize sizeFromContents(ExCustomStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const;
|
||||
// inline int styleHint(ExCustomStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const;
|
||||
|
||||
// inline void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const; //这三个不用写成内联函数,且会编译不通过
|
||||
// inline QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const;
|
||||
// inline QStyle::SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt, const QWidget *widget) const;
|
||||
// virtual void polish(QPalette &palette) override; //通常在此函数内指定配色方案,也即配置调色板
|
||||
// virtual void polish(QWidget *widget) override; //当样式应用到窗口部件时,polish(QWidget*)就会调用,从而允许我们进行最后的定制
|
||||
// virtual void unpolish(QWidget *widget) override; //当动态改变样式的时候,unpolish就会调用,来撤销polish的影响。polish(QWidget*)一般用做窗口部件的事件过滤器。
|
||||
|
||||
// QStyle interface 重写QCommonStyle重载的函数
|
||||
public:
|
||||
virtual void polish(QWidget *widget) override;
|
||||
virtual void unpolish(QWidget *widget) override;
|
||||
virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const override;
|
||||
virtual void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const override;
|
||||
virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const override;
|
||||
virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget) const override;
|
||||
virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const override;
|
||||
virtual int styleHint(StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const override;
|
||||
|
||||
|
||||
//重实现,使得父类的多个同名 函数 (不同参数) 任然可以在本类里面使用
|
||||
using QCommonStyle::drawPrimitive;
|
||||
using QCommonStyle::drawControl;
|
||||
using QCommonStyle::subElementRect;
|
||||
using QCommonStyle::pixelMetric;
|
||||
using QCommonStyle::sizeFromContents;
|
||||
using QCommonStyle::styleHint;
|
||||
using QCommonStyle::standardIcon;
|
||||
|
||||
public:
|
||||
};
|
||||
EXWIDGET_END_NAMESPACE
|
||||
|
||||
#endif // EXMYSTYLE_H
|
@ -1,92 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#include "Example.h"
|
||||
|
||||
#include <QStyleFactory>
|
||||
#include <QDebug>
|
||||
#include <QTableWidget>
|
||||
#include <QProgressBar>
|
||||
#include <QPushButton>
|
||||
#include <QApplication>
|
||||
#include <QSlider>
|
||||
#include <QScrollBar>
|
||||
|
||||
#include "MyWidget/ExSwitchButton.h"
|
||||
|
||||
EXWIDGET_USE_NAMESPACE
|
||||
|
||||
Example::Example(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Example::~Example()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Example::init()
|
||||
{
|
||||
QTableWidget *table = new QTableWidget(10, 10, this);
|
||||
QScrollBar *scrollBarH = new QScrollBar(this);
|
||||
QScrollBar *scrollBarV = new QScrollBar(this);
|
||||
|
||||
table->move(10, 10);
|
||||
|
||||
scrollBarH->move(300, 50);
|
||||
scrollBarH->setRange(0, 100);
|
||||
scrollBarH->setValue(34);
|
||||
scrollBarH->resize(380, 20);
|
||||
scrollBarH->setOrientation(Qt::Horizontal);
|
||||
|
||||
scrollBarV->move(50, 250);
|
||||
scrollBarV->setRange(0, 100);
|
||||
scrollBarV->setValue(67);
|
||||
scrollBarV->resize(20, 380);
|
||||
scrollBarV->setOrientation(Qt::Vertical);
|
||||
|
||||
int i = 0;
|
||||
QStringList listStyle = QStyleFactory::keys();
|
||||
foreach(QString val, listStyle) { //打印当前系统支持的系统风格
|
||||
QPushButton *btn = new QPushButton(val, this);
|
||||
btn->move(this->rect().right() - 100, this->rect().top() + i++ * 40);
|
||||
connect(btn, &QPushButton::clicked, this, [=](){
|
||||
qApp->setStyle(val);
|
||||
});
|
||||
}
|
||||
|
||||
QPushButton *btn = new QPushButton("ExMyStyle", this);
|
||||
btn->move(this->rect().right() - 100, this->rect().top() + i++ * 40);
|
||||
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();
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#ifndef EXAMPLE_H
|
||||
#define EXAMPLE_H
|
||||
|
||||
#include <QWidget> //Qt的库
|
||||
|
||||
#include "ExMyStyle.h" //自己的库
|
||||
|
||||
class Example : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Example(QWidget *parent = 0);
|
||||
~Example();
|
||||
|
||||
void init();
|
||||
};
|
||||
|
||||
#endif // EXAMPLE_H
|
@ -1,105 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#include "ExSwitchButton.h"
|
||||
#include "ExSwitchButton_p.h"
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include "ExMyStyle.h"
|
||||
|
||||
EXWIDGET_BEGIN_NAMESPACE
|
||||
|
||||
//xxxPrivate class 会在 xxx.cpp 里面来实现
|
||||
/*!
|
||||
* \~chinese \brief ExSwitchButtonPrivate 的构造函数
|
||||
*/
|
||||
ExSwitchButtonPrivate::ExSwitchButtonPrivate(/*ExSwitchButton *qq*/)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
ExSwitchButtonPrivate::~ExSwitchButtonPrivate()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExSwitchButtonPrivate::init()
|
||||
{
|
||||
m_checked = false; //xxxxPrivate 自带的变量
|
||||
Q_Q(ExSwitchButton); //Q_Q 可以在 xxxxPrivate 里面调用 xxxx 的方法和变量
|
||||
|
||||
q->setObjectName("ExSwitchButtonPrivate");
|
||||
q->setCheckable(true);
|
||||
q->connect(q, SIGNAL(toggled(bool)), q, SLOT(onChange(bool)));
|
||||
|
||||
q->onChange(true);
|
||||
}
|
||||
|
||||
ExSwitchButton::ExSwitchButton()
|
||||
{
|
||||
Q_D(ExSwitchButton);
|
||||
}
|
||||
|
||||
ExSwitchButton::~ExSwitchButton()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ExSwitchButton::onChange(bool)
|
||||
{
|
||||
Q_D(ExSwitchButton);
|
||||
|
||||
d->m_checked = (!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
|
@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#ifndef EXSWITCHBUTTON_H
|
||||
#define EXSWITCHBUTTON_H
|
||||
|
||||
#include "ExMyGlobal.h"
|
||||
#include <QtGlobal>
|
||||
|
||||
#include <QAbstractButton>
|
||||
#include <QStyleOption>
|
||||
//#include <QStyleOptionButton>
|
||||
|
||||
EXWIDGET_BEGIN_NAMESPACE
|
||||
|
||||
class ExSwitchButtonPrivate; //Q_DECLARE_PRIVATE() 宏展开使用的
|
||||
class ExSwitchButton : public QAbstractButton
|
||||
{
|
||||
public:
|
||||
explicit ExSwitchButton();
|
||||
~ExSwitchButton();
|
||||
|
||||
private slots:
|
||||
void onChange(bool);
|
||||
|
||||
private:
|
||||
Q_DECLARE_PRIVATE(ExSwitchButton)
|
||||
|
||||
// QWidget interface
|
||||
protected:
|
||||
virtual void paintEvent(QPaintEvent *event) override; //必须重载的纯虚函数
|
||||
void initStyleOption(QStyleOptionButton* option) const;
|
||||
|
||||
};
|
||||
|
||||
EXWIDGET_END_NAMESPACE
|
||||
|
||||
#endif
|
@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#ifndef EXSWITCHBUTTON_P_H
|
||||
#define EXSWITCHBUTTON_P_H
|
||||
|
||||
#include "ExMyGlobal.h"
|
||||
#include <QWidget>
|
||||
#include <QtGlobal>
|
||||
#include <private/qobject_p.h>
|
||||
|
||||
EXWIDGET_BEGIN_NAMESPACE
|
||||
|
||||
/*!
|
||||
* \~chinese \class ExSwitchButtonPrivate
|
||||
* \~chinese \brief ExSwitchButton 类的数据类, 便于实现源码/二进制兼容, 通常继承于 “自定义_Private” 的类
|
||||
* 若是自定义控件,没有继承于 QObjectPrivate 之类的, 使用 Q_Q Q_D 时候,会报错,需要自己定义 q_fun() 函数
|
||||
* \~chinese \sa Qt 自带的 QPushButtonPrivate, QPushButton 实现
|
||||
*/
|
||||
|
||||
|
||||
class ExSwitchButtonPrivate : public QObjectPrivate
|
||||
{
|
||||
public:
|
||||
explicit ExSwitchButtonPrivate();
|
||||
~ExSwitchButtonPrivate();
|
||||
|
||||
// virtual ~QObjectData();
|
||||
|
||||
void init();
|
||||
|
||||
public:
|
||||
bool m_checked; //switch 是否处于开启状态
|
||||
|
||||
public:
|
||||
Q_DECLARE_PUBLIC(ExSwitchButton) //要加上宏
|
||||
};
|
||||
|
||||
EXWIDGET_END_NAMESPACE
|
||||
|
||||
#endif
|
@ -1,45 +0,0 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2019-11-07T16:47:52
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
#xxxPrivate 继承 Q-xxxx-Private的话,是需要的添加 core-private(有一些核心方法? 类 可能不开放)
|
||||
QT += core gui core-private
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets widgets-private # 若是需要继承 widgets相关,需要添加 widgets-private
|
||||
|
||||
TARGET = custom-style
|
||||
TEMPLATE = app
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any feature of Qt which has been marked as deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if you use deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
Example.cpp \
|
||||
ExMyStyle.cpp \
|
||||
MyWidget/ExSwitchButton.cpp
|
||||
|
||||
HEADERS += \
|
||||
Example.h \
|
||||
ExMyStyle.h \
|
||||
ExMyGlobal.h \
|
||||
MyWidget/ExSwitchButton.h \
|
||||
MyWidget/ExSwitchButton_p.h
|
||||
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 4.2 KiB |
@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
|
||||
*
|
||||
* Author: touwoyimuli <touwoyimuli@gmai.com>
|
||||
*
|
||||
* github: https://github.com/touwoyimuli
|
||||
* blogs: https://touwoyimuli.github.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://touwoyimuli.github.io/>.
|
||||
*/
|
||||
#include "Example.h"
|
||||
#include <QApplication>
|
||||
#include <QStyleFactory>
|
||||
#include <QDebug>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
||||
//若为系统自带的QStyle,则qApp->setStyle("系统自带风格");
|
||||
//若为自定义新的QStyle,则qApp->setStyle(QStyleFactory::create("自定义风格"))
|
||||
QStringList listStyle = QStyleFactory::keys();
|
||||
foreach(QString val, listStyle) //打印当前系统支持的系统风格
|
||||
qDebug()<<val<<" ";
|
||||
|
||||
qApp->setStyle("chameleon"); //Windows/Fusion/...
|
||||
Example *w = new Example();
|
||||
w->resize(1000, 700);
|
||||
w->init();
|
||||
w->show();
|
||||
|
||||
return a.exec();
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
#include "Example.h"
|
||||
|
||||
Example::Example(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
}
|
||||
|
||||
Example::~Example()
|
||||
{
|
||||
}
|
||||
|
@ -1,14 +0,0 @@
|
||||
#ifndef EXAMPLE_H
|
||||
#define EXAMPLE_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
class Example : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Example(QWidget *parent = nullptr);
|
||||
~Example();
|
||||
};
|
||||
#endif // EXAMPLE_H
|
@ -65,31 +65,11 @@ QRect MyStyle::subElementRect(MyStyle::SubElement subElement, const QStyleOption
|
||||
return proxy()->subElementRect(static_cast<QStyle::SubElement>(subElement), option, widget);
|
||||
}
|
||||
|
||||
//void MyStyle::drawComplexControl(MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
//QRect MyStyle::subControlRect(MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const
|
||||
//{
|
||||
// return QRect();
|
||||
//}
|
||||
|
||||
int MyStyle::pixelMetric(MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
|
||||
{
|
||||
return proxy()->pixelMetric(static_cast<QStyle::PixelMetric>(metric), option, widget);
|
||||
}
|
||||
|
||||
//QSize MyStyle::sizeFromContents(MyStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const
|
||||
//{
|
||||
// return QSize();
|
||||
//}
|
||||
|
||||
//int MyStyle::styleHint(MyStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const
|
||||
//{
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
void MyStyle::polish(QWidget *widget)
|
||||
{
|
||||
QCommonStyle::polish(widget);
|
||||
|
@ -64,22 +64,14 @@ public:
|
||||
static void drawPrimitive(const QStyle *style, MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = nullptr);
|
||||
static void drawControl(const QStyle *style, MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w);
|
||||
static QRect subElementRect(const QStyle *style, MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget);
|
||||
// static void drawComplexControl(const QStyle *style, MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const;
|
||||
// static QRect subControlRect(const QStyle *style, MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const;
|
||||
static int pixelMetric(const QStyle *style, MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget);
|
||||
// static QSize sizeFromContents(const QStyle *style, MyStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const;
|
||||
// static int styleHint(const QStyle *style, MyStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const;
|
||||
|
||||
//新增加的枚举属 MyStyle:: , 之能够在此内敛函数里面调用
|
||||
//[主要用来绘画 自定义新增 的控件枚举 --> 实际调用在 下面的 virtual 里面绘画]
|
||||
inline void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w = nullptr) const;
|
||||
inline void drawControl(MyStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const;
|
||||
inline QRect subElementRect(MyStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const;
|
||||
// inline void drawComplexControl(MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const;
|
||||
// inline QRect subControlRect(MyStyle::ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const;
|
||||
inline int pixelMetric(MyStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const;
|
||||
// inline QSize sizeFromContents(MyStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const;
|
||||
// inline int styleHint(MyStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const;
|
||||
|
||||
//这里的快捷方式创建的枚举,都是不带QStyle:: ; 但是快捷方式的定义是带是QStyle:: , 此处声明的地方必须加上 QStyle:: /*后面改写更复杂的得写上MyStyle:: 因添加自定义的枚举*/
|
||||
//这里 override 的虚函数,只能够调用旧有的 QStyle:: 的函数
|
||||
@ -135,10 +127,10 @@ public:
|
||||
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);
|
||||
void drawPrimitive(MyStyle::PrimitiveElement pe, const QStyleOption *opt);
|
||||
void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt);
|
||||
void drawControl(MyStyle::ControlElement element, const QStyleOption *opt);
|
||||
void drawControl(QStyle::ControlElement element, const QStyleOption *opt);
|
||||
|
||||
private:
|
||||
QWidget* m_widget;
|
||||
|
@ -67,7 +67,7 @@ void MySwitchButtonPrivate::init()
|
||||
check = false;
|
||||
q->setObjectName("MySwitchButton");
|
||||
q->setChecked(true);
|
||||
q->setCheckable(true); //clicked toggled douxuyaokauqi
|
||||
q->setCheckable(true); //clicked toggled 都需要开启
|
||||
q->connect(q, SIGNAL(clicked(bool)), q, SLOT(setChecked(bool)));
|
||||
}
|
||||
|
||||
|
@ -1,28 +0,0 @@
|
||||
QT += core gui
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any Qt feature that has been marked deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if it uses deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
Example.cpp
|
||||
|
||||
HEADERS += \
|
||||
Example.h
|
||||
|
||||
# Default rules for deployment.
|
||||
qnx: target.path = /tmp/$${TARGET}/bin
|
||||
else: unix:!android: target.path = /opt/$${TARGET}/bin
|
||||
!isEmpty(target.path): INSTALLS += target
|
@ -1,11 +0,0 @@
|
||||
#include "Example.h"
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
Example w;
|
||||
w.show();
|
||||
return a.exec();
|
||||
}
|
Loading…
Reference in New Issue
Block a user