From 16b00f6d4b493a206c5641ed719d5cc2a718adc2 Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Wed, 4 Dec 2019 00:21:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20v1.3=20=E4=BD=BF=E7=94=A8=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89MyStyle=E9=A3=8E=E6=A0=BC=EF=BC=8C=E4=BB=A5Sc?= =?UTF-8?q?rollBar=E6=8E=A7=E4=BB=B6=E4=B8=BA=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtCustomStyleEx/ExMyGlobal.h | 35 ++++++++ QtCustomStyleEx/ExMyStyle.cpp | 124 ++++++++++++++++++++++++++++ QtCustomStyleEx/ExMyStyle.h | 21 +++++ QtCustomStyleEx/Example.cpp | 36 +++++--- QtCustomStyleEx/Example.h | 10 +-- QtCustomStyleEx/QtCustomStyleEx.pro | 5 +- 6 files changed, 209 insertions(+), 22 deletions(-) create mode 100644 QtCustomStyleEx/ExMyGlobal.h diff --git a/QtCustomStyleEx/ExMyGlobal.h b/QtCustomStyleEx/ExMyGlobal.h new file mode 100644 index 0000000..fe69a87 --- /dev/null +++ b/QtCustomStyleEx/ExMyGlobal.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved. + * + * Author: touwoyimuli + * + * 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 . + */ +#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 diff --git a/QtCustomStyleEx/ExMyStyle.cpp b/QtCustomStyleEx/ExMyStyle.cpp index 978c7f1..fd89728 100644 --- a/QtCustomStyleEx/ExMyStyle.cpp +++ b/QtCustomStyleEx/ExMyStyle.cpp @@ -21,7 +21,131 @@ */ #include "ExMyStyle.h" +#include +#include +#include +#include +#include + +EXWIDGET_BEGIN_NAMESPACE ExMyStyle::ExMyStyle() { } + +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----:"< +QT_BEGIN_NAMESPACE +//class QCommonStyle; +QT_END_NAMESPACE +EXWIDGET_BEGIN_NAMESPACE class ExMyStyle : public QCommonStyle { public: ExMyStyle(); + + // QStyle interface +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; + +public: }; +EXWIDGET_END_NAMESPACE #endif // EXMYSTYLE_H diff --git a/QtCustomStyleEx/Example.cpp b/QtCustomStyleEx/Example.cpp index 08eb033..4136678 100644 --- a/QtCustomStyleEx/Example.cpp +++ b/QtCustomStyleEx/Example.cpp @@ -21,6 +21,17 @@ */ #include "Example.h" +#include +#include +#include +#include +#include +#include +#include +#include + +EXWIDGET_USE_NAMESPACE + Example::Example(QWidget *parent) : QWidget(parent) { @@ -35,21 +46,22 @@ Example::~Example() void Example::init() { QTableWidget *table = new QTableWidget(10, 10, this); - QProgressBar *progressH = new QProgressBar(this); - QProgressBar *progressV = new QProgressBar(this); + QScrollBar *scrollBarH = new QScrollBar(this); + QScrollBar *scrollBarV = new QScrollBar(this); table->move(10, 10); - progressH->move(300, 50); - progressH->setRange(0, 100); - progressH->setValue(34); - progressH->resize(380, 40); + scrollBarH->move(300, 50); + scrollBarH->setRange(0, 100); + scrollBarH->setValue(34); + scrollBarH->resize(380, 20); + scrollBarH->setOrientation(Qt::Horizontal); - progressV->move(50, 250); - progressV->setRange(0, 100); - progressV->setValue(67); - progressV->resize(50, 380); - progressV->setOrientation(Qt::Vertical); + 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(); @@ -64,6 +76,6 @@ void Example::init() QPushButton *btn = new QPushButton("ExMyStyle", this); btn->move(this->rect().right() - 100, this->rect().top() + i++ * 40); connect(btn, &QPushButton::clicked, this, [=](){ - qApp->setStyle(QStyleFactory::create("ExMyStyle")); + qApp->setStyle(new ExMyStyle()); }); } diff --git a/QtCustomStyleEx/Example.h b/QtCustomStyleEx/Example.h index 82ef9e8..dc0e8c9 100644 --- a/QtCustomStyleEx/Example.h +++ b/QtCustomStyleEx/Example.h @@ -22,15 +22,9 @@ #ifndef EXAMPLE_H #define EXAMPLE_H -#include -#include -#include -#include -#include -#include -#include +#include //Qt的库 -#include "ExMyStyle.h" +#include "ExMyStyle.h" //自己的库 class Example : public QWidget { diff --git a/QtCustomStyleEx/QtCustomStyleEx.pro b/QtCustomStyleEx/QtCustomStyleEx.pro index 181f14d..f488713 100644 --- a/QtCustomStyleEx/QtCustomStyleEx.pro +++ b/QtCustomStyleEx/QtCustomStyleEx.pro @@ -8,7 +8,7 @@ QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets -TARGET = QtCustomStyleEx +TARGET = custom-style TEMPLATE = app # The following define makes your compiler emit warnings if you use @@ -31,7 +31,8 @@ SOURCES += \ HEADERS += \ Example.h \ - ExMyStyle.h + ExMyStyle.h \ + ExMyGlobal.h # Default rules for deployment.