diff --git a/QtCustomStyleEx/ExCustomStyle.cpp b/QtCustomStyleEx/ExCustomStyle.cpp index e87fbb3..d3ef99e 100644 --- a/QtCustomStyleEx/ExCustomStyle.cpp +++ b/QtCustomStyleEx/ExCustomStyle.cpp @@ -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 diff --git a/QtCustomStyleEx/ExCustomStyle.h b/QtCustomStyleEx/ExCustomStyle.h index b6cc511..cc96b67 100644 --- a/QtCustomStyleEx/ExCustomStyle.h +++ b/QtCustomStyleEx/ExCustomStyle.h @@ -1,18 +1,37 @@ #ifndef EXCUSTOMSTYLE_H #define EXCUSTOMSTYLE_H +//Qt自带的库 #include +#include +#include +//自定义的文件或库 #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 diff --git a/QtCustomStyleEx/ExDefineGlobal.h b/QtCustomStyleEx/ExDefineGlobal.h index d0d4d79..9aee8ad 100644 --- a/QtCustomStyleEx/ExDefineGlobal.h +++ b/QtCustomStyleEx/ExDefineGlobal.h @@ -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 diff --git a/QtCustomStyleEx/Examples.cpp b/QtCustomStyleEx/Examples.cpp index b3c6912..d73a19f 100644 --- a/QtCustomStyleEx/Examples.cpp +++ b/QtCustomStyleEx/Examples.cpp @@ -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()); }