feat: Build the custom redraw frame, eg: QScrollBar

搭建自定义QStyle重绘的框架,使用系统自带的和滚动条QScrollBar为示范
This commit is contained in:
touwoyimuli 2019-09-10 22:36:14 +08:00
parent 7478bb8f34
commit 56ac97ca4c
4 changed files with 94 additions and 2 deletions

View File

@ -4,7 +4,74 @@ CUSTOMSTYLE_BEDGIN_NAMESPACE
ExCustomStyle::ExCustomStyle()
{
}
//重写重载的函数+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void ExCustomStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
QCommonStyle::drawPrimitive(pe, opt, p, w);
}
void ExCustomStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const
{
switch (element) {
//滚动条+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
case CE_ScrollBarAddPage: { //增加页(在滑槽)
p->fillRect(opt->rect, Qt::green);
break;
}
case CE_ScrollBarSubPage: { //减少页(在滑槽)
p->fillRect(opt->rect, Qt::gray);
break;
}
case CE_ScrollBarSlider: { //滑块
p->fillRect(opt->rect, Qt::red);
break;
}
case CE_ScrollBarAddLine: { //增加按钮
p->fillRect(opt->rect, Qt::blue);
break;
}
case CE_ScrollBarSubLine: { //减少按钮
p->fillRect(opt->rect, Qt::magenta);
break;
}
// case CE_ScrollBarFirst:
// case CE_ScrollBarLast: break; //未知
//END+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
default:
break;
}
QCommonStyle::drawControl(element, opt, p, w);
}
QRect ExCustomStyle::subElementRect(QStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const
{
return QCommonStyle::subElementRect(subElement, option, widget);
}
int ExCustomStyle::pixelMetric(QStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const
{
return QCommonStyle::pixelMetric(metric, option, widget);
}
QSize ExCustomStyle::sizeFromContents(QStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const
{
return QCommonStyle::sizeFromContents(ct, opt, contentsSize, w);
}
int ExCustomStyle::styleHint(QStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const
{
return QCommonStyle::styleHint(stylehint, opt, widget, returnData);
}
QIcon ExCustomStyle::standardIcon(QStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const
{
return QCommonStyle::standardIcon(standardIcon, option, widget);
}
//END+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
CUSTOMSTYLE_END_NAMESPACE

View File

@ -1,18 +1,37 @@
#ifndef EXCUSTOMSTYLE_H
#define EXCUSTOMSTYLE_H
//Qt自带的库
#include <QCommonStyle>
#include <QPainter>
#include <QStyleOption>
//自定义的文件或库
#include "ExDefineGlobal.h"
CUSTOMSTYLE_BEDGIN_NAMESPACE
//一些工具函数:绘画圈,叉,对号,阴影等功能函数
CUSTOM_BEGIN_NAMESPACE
CUSTOMSTYLE_END_NAMESPACE
class ExCustomStyle : public QCommonStyle
{
Q_OBJECT
public:
ExCustomStyle();
public: //重写QCommonStyle重载的函数
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 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;
virtual QIcon standardIcon(StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const override;
public:
};
CUSTOMSTYLE_END_NAMESPACE

View File

@ -7,7 +7,12 @@
#define CUSTOMSTYLE_BEDGIN_NAMESPACE namespace touwoyimuliStyle {
#define CUSTOMSTYLE_END_NAMESPACE }
#define CUSTOM_BEGIN_NAMESPACE namespace touwoyimuliDrawUtils { //嵌套于命名空间 touwoyimuliDrawUtils里面
#define CUSTOM_END_NAMESPACE }
//定义使用命名空间((在使用 class ExCustomStyle的定义内容时候用到了 eg:Examples.cpp里面))
#define CUSTOMSTYLE_USE_NAMESPACE using namespace touwoyimuliStyle;
#endif // EXDEFINEGLOBAL_H

View File

@ -30,7 +30,8 @@ void Examples::init()
//设置一个默认的风格
ui->comboBox->addItem("ExCustomStyle");
qApp->setStyle(QStyleFactory::create("Fusion"));
// qApp->setStyle(QStyleFactory::create("Fusion"));
qApp->setStyle(new ExCustomStyle());
}