diff --git a/QtMyStyleEx/QtExample03/QtStyleEx/QtStyleEx.pro b/QtMyStyleEx/QtExample03/QtStyleEx/QtStyleEx.pro new file mode 100644 index 0000000..c9dad30 --- /dev/null +++ b/QtMyStyleEx/QtExample03/QtStyleEx/QtStyleEx.pro @@ -0,0 +1,59 @@ +# +# 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. +# + +#------------------------------------------------- +# +# Project created by QtCreator 2020-02-13T22:45:19 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtStyleEx +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 \ + widget.cpp \ + mystyle.cpp + +HEADERS += \ + widget.h \ + mystyle.h + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtMyStyleEx/QtExample03/QtStyleEx/main.cpp b/QtMyStyleEx/QtExample03/QtStyleEx/main.cpp new file mode 100644 index 0000000..33f5dc1 --- /dev/null +++ b/QtMyStyleEx/QtExample03/QtStyleEx/main.cpp @@ -0,0 +1,32 @@ +/* + * + * 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. + */ + +#include "widget.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + + return a.exec(); +} diff --git a/QtMyStyleEx/QtExample03/QtStyleEx/mystyle.cpp b/QtMyStyleEx/QtExample03/QtStyleEx/mystyle.cpp new file mode 100644 index 0000000..d0378df --- /dev/null +++ b/QtMyStyleEx/QtExample03/QtStyleEx/mystyle.cpp @@ -0,0 +1,112 @@ +/* + * + * 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. + */ + +#include "mystyle.h" + +#include +#include +#include + +MyStyle::MyStyle() +{ + +} + +void MyStyle::polish(QWidget *widget) +{ + QCommonStyle::polish(widget); +} + +void MyStyle::unpolish(QWidget *widget) +{ + QCommonStyle::unpolish(widget); +} + +void MyStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const +{ + QCommonStyle::drawPrimitive(pe, opt, p, w); +} + +void MyStyle::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; + } + + return QCommonStyle::drawControl(element, opt, p, w); +} + +QRect MyStyle::subElementRect(QStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const +{ + return QCommonStyle::subElementRect(subElement, option, widget); +} + +void MyStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const +{ + QCommonStyle::drawComplexControl(cc, opt, p, widget); +} + +QRect MyStyle::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QStyle::SubControl sc, const QWidget *widget) const +{ + return QCommonStyle::subControlRect(cc, opt, sc, widget); +} + +int MyStyle::pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const +{ + return QCommonStyle::pixelMetric(metric, option, widget); +} + +QSize MyStyle::sizeFromContents(QStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const +{ + return QCommonStyle::sizeFromContents(ct, opt, contentsSize, w); +} + +int MyStyle::styleHint(QStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const +{ + return QCommonStyle::styleHint(stylehint, opt, widget, returnData); +} + + + + diff --git a/QtMyStyleEx/QtExample03/QtStyleEx/mystyle.h b/QtMyStyleEx/QtExample03/QtStyleEx/mystyle.h new file mode 100644 index 0000000..acf8031 --- /dev/null +++ b/QtMyStyleEx/QtExample03/QtStyleEx/mystyle.h @@ -0,0 +1,47 @@ +/* + * + * 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 MYSTYLE_H +#define MYSTYLE_H + +#include + +class MyStyle : public QCommonStyle +{ +public: + MyStyle(); + + // QStyle interface +public: + virtual void polish(QWidget *widget) override; + virtual void unpolish(QWidget *widget) override; + virtual void drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override; + virtual void drawControl(ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const override; + virtual QRect subElementRect(SubElement subElement, const QStyleOption *option, const QWidget *widget) const override; + virtual void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const override; + virtual QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const override; + virtual int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const override; + virtual QSize sizeFromContents(ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const override; + virtual int styleHint(StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const override; + +}; + +#endif // MYSTYLE_H diff --git a/QtMyStyleEx/QtExample03/QtStyleEx/widget.cpp b/QtMyStyleEx/QtExample03/QtStyleEx/widget.cpp new file mode 100644 index 0000000..0bf5349 --- /dev/null +++ b/QtMyStyleEx/QtExample03/QtStyleEx/widget.cpp @@ -0,0 +1,98 @@ +/* + * + * 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. + */ + +#include "widget.h" +#include "mystyle.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +Widget::Widget(QWidget *parent) + : QWidget(parent) +{ + init(); +} + +Widget::~Widget() +{ + +} + +void Widget::init() +{ + resize(1000, 600); + + QTableWidget *table = new QTableWidget(10, 10, this); + table->move(10, 10); + + QScrollBar *scrollBarH = new QScrollBar(this); + scrollBarH->move(300, 50); + scrollBarH->setRange(0, 100); + scrollBarH->setValue(34); + scrollBarH->resize(380, 20); + scrollBarH->setOrientation(Qt::Horizontal); + + QScrollBar *scrollBarV = new QScrollBar(this); + scrollBarV->move(50, 250); + scrollBarV->setRange(0, 100); + scrollBarV->setValue(67); + scrollBarV->resize(20, 380); + scrollBarV->setOrientation(Qt::Vertical); + + QProgressBar* progreH = new QProgressBar(this); + progreH->move(300, 100); + progreH->resize(250, 40); + progreH->setValue(37); + progreH->setOrientation(Qt::Horizontal); + + QProgressBar* progreV = new QProgressBar(this); + progreV->move(100, 300); + progreV->resize(40, 250); + progreV->setValue(67); + progreV->setOrientation(Qt::Vertical); + + int i = 0; + QStringList listStyle = QStyleFactory::keys(); + + //显示 and 使用当前系统支持的系统风格 + foreach(QString val, listStyle) { + qDebug()<move(this->rect().right() - 100, this->rect().top() + i++ * 40); + connect(btn, &QPushButton::clicked, this, [=](){ + qApp->setStyle(val); + }); + } + + //若是自定义风格, new 为对象即可 + QPushButton *btn = new QPushButton("My Style", this); + btn->move(this->rect().right() - 100, this->rect().top() + i++ * 40); + connect(btn, &QPushButton::clicked, this, [=](){ + qApp->setStyle(new MyStyle()); + }); + +} diff --git a/QtMyStyleEx/QtExample03/QtStyleEx/widget.h b/QtMyStyleEx/QtExample03/QtStyleEx/widget.h new file mode 100644 index 0000000..e8080b3 --- /dev/null +++ b/QtMyStyleEx/QtExample03/QtStyleEx/widget.h @@ -0,0 +1,38 @@ +/* + * + * 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 WIDGET_H +#define WIDGET_H + +#include + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = 0); + ~Widget(); + + void init(); +}; + +#endif // WIDGET_H