feat: v1.3 使用自定义MyStyle风格,以ScrollBar控件为显示
This commit is contained in:
parent
e4872abf51
commit
16b00f6d4b
35
QtCustomStyleEx/ExMyGlobal.h
Normal file
35
QtCustomStyleEx/ExMyGlobal.h
Normal file
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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
|
@ -21,7 +21,131 @@
|
||||
*/
|
||||
#include "ExMyStyle.h"
|
||||
|
||||
#include <QProxyStyle>
|
||||
#include <QDebug>
|
||||
#include <QPainter>
|
||||
#include <QStyleOption>
|
||||
#include <QLinearGradient>
|
||||
|
||||
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----:"<<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
|
||||
|
@ -22,13 +22,34 @@
|
||||
#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
|
||||
{
|
||||
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
|
||||
|
@ -21,6 +21,17 @@
|
||||
*/
|
||||
#include "Example.h"
|
||||
|
||||
#include <QStyleFactory>
|
||||
#include <QDebug>
|
||||
#include <QTableWidget>
|
||||
#include <QProgressBar>
|
||||
#include <QPushButton>
|
||||
#include <QApplication>
|
||||
#include <QSlider>
|
||||
#include <QScrollBar>
|
||||
|
||||
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());
|
||||
});
|
||||
}
|
||||
|
@ -22,15 +22,9 @@
|
||||
#ifndef EXAMPLE_H
|
||||
#define EXAMPLE_H
|
||||
|
||||
#include <QStyleFactory>
|
||||
#include <QDebug>
|
||||
#include <QWidget>
|
||||
#include <QTableWidget>
|
||||
#include <QProgressBar>
|
||||
#include <QPushButton>
|
||||
#include <QApplication>
|
||||
#include <QWidget> //Qt的库
|
||||
|
||||
#include "ExMyStyle.h"
|
||||
#include "ExMyStyle.h" //自己的库
|
||||
|
||||
class Example : public QWidget
|
||||
{
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user