From 788e560190783819432103274ed5d278b0ab37ba Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Sat, 28 Sep 2019 23:59:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=BF=85=E9=A1=BB?= =?UTF-8?q?=E5=86=99=E7=9A=84=E5=87=A0=E4=B8=AA=E5=86=85=E8=81=94=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=92=8C=E9=9D=99=E6=80=81=E5=87=BD=E6=95=B0=E4=BB=A5?= =?UTF-8?q?=E5=8F=8A=E9=87=8D=E5=86=99=E7=9A=84=E8=99=9A=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QtCustomStyleEx/ExCustomStyle.cpp | 92 ++++++++++++++++++++++++++++++- QtCustomStyleEx/ExCustomStyle.h | 33 +++++++++-- 2 files changed, 120 insertions(+), 5 deletions(-) diff --git a/QtCustomStyleEx/ExCustomStyle.cpp b/QtCustomStyleEx/ExCustomStyle.cpp index 5d5b7f2..adb7498 100644 --- a/QtCustomStyleEx/ExCustomStyle.cpp +++ b/QtCustomStyleEx/ExCustomStyle.cpp @@ -7,7 +7,76 @@ ExCustomStyle::ExCustomStyle() } -//重写重载的函数+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//静态static的函数+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +void ExCustomStyle::drawPrimitive(const QStyle *style, ExCustomStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) +{ +// DStyleHelper dstyle(style); + + switch (pe) { +// case value: +// break; + default: + break; + } +} + +void ExCustomStyle::drawControl(const QStyle *style, ExCustomStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) +{ +} + +QRect ExCustomStyle::subElementRect(const QStyle *style, ExCustomStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) +{ + return QRect(); +} + +QSize ExCustomStyle::sizeFromContents(const QStyle *style, ExCustomStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) +{ + return QSize(); +} + +QIcon ExCustomStyle::standardIcon(const QStyle *style, ExCustomStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) +{ + return QIcon(); +} + +//内联inline的函数+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +//实际上,只需要在实现处添加inline,声明出不需要写inline +inline void ExCustomStyle::drawPrimitive(ExCustomStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const +{ + proxy()->drawPrimitive(static_cast(pe), opt, p, w); +} + +inline void ExCustomStyle::drawControl(ExCustomStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const +{ + proxy()->drawControl(static_cast(element), opt, p, w); +} + +inline QRect ExCustomStyle::subElementRect(ExCustomStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const +{ + proxy()->subElementRect(static_cast(subElement), option, widget); +} + +inline int ExCustomStyle::pixelMetric(ExCustomStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const +{ + proxy()->pixelMetric(static_cast(metric), option, widget); +} + +inline QSize ExCustomStyle::sizeFromContents(ExCustomStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const +{ + proxy()->sizeFromContents(static_cast(ct), opt, contentsSize, w); +} + +inline int ExCustomStyle::styleHint(ExCustomStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const +{ + proxy()->styleHint(static_cast(stylehint), opt, widget, returnData); +} + +inline QIcon ExCustomStyle::standardIcon(ExCustomStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const +{ + proxy()->standardIcon(static_cast(standardIcon), option, widget); +} + +//重写重载的函数==================================================================================================== void ExCustomStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const { QCommonStyle::drawPrimitive(pe, opt, p, w); @@ -15,6 +84,12 @@ void ExCustomStyle::drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOptio void ExCustomStyle::drawControl(QStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const { +// if (Q_UNLIKELY(element < QStyle::CE_CustomBase)) { +// return QCommonStyle::drawControl(element, opt, p, w); +// } + +// drawControl(this, static_cast(element), opt, p, w); + switch (element) { //滚动条------------------------------------- case CE_ScrollBarAddPage: { //增加页(在滑槽) @@ -73,6 +148,21 @@ QIcon ExCustomStyle::standardIcon(QStyle::StandardPixmap standardIcon, const QSt return QCommonStyle::standardIcon(standardIcon, option, widget); } +void ExCustomStyle::drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const +{ + return QCommonStyle::drawComplexControl(cc, opt, p, widget); +} + +QRect ExCustomStyle::subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const +{ + return QCommonStyle::subControlRect(cc, opt, sc, widget); +} + +QStyle::SubControl ExCustomStyle::hitTestComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt, const QWidget *widget) const +{ + return QCommonStyle::hitTestComplexControl(cc, opt, pt, widget); +} + //END+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ CUSTOMSTYLE_END_NAMESPACE diff --git a/QtCustomStyleEx/ExCustomStyle.h b/QtCustomStyleEx/ExCustomStyle.h index fac0c2b..af52d77 100644 --- a/QtCustomStyleEx/ExCustomStyle.h +++ b/QtCustomStyleEx/ExCustomStyle.h @@ -79,12 +79,25 @@ public: }; Q_DECLARE_FLAGS(StateFlags, StyleState) //而类型定义(typedef)和操作符重载正是宏Q_DECLARE_FLAGS 和 Q_DECLARE_OPERATORS_FOR_FLAGS (均无关于元对象系统) - - //内联函数,然后自己选择是调用静态函数,还是重写的基类的函数 - inline void drawPrimitive(ExCustomStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const - { proxy()->drawPrimitive(static_cast(pe), opt, p, w); } + //静态static函数 + static void drawPrimitive(const QStyle *style, ExCustomStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w); + static void drawControl(const QStyle *style, ExCustomStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w); + static QRect subElementRect(const QStyle *style, ExCustomStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget); + static QSize sizeFromContents(const QStyle *style, ExCustomStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w); + static QIcon standardIcon(const QStyle *style, ExCustomStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget); + //内联inline函数,然后自己选择是调用静态函数,还是重写的基类的函数 + inline void drawPrimitive(ExCustomStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const; + inline void drawControl(ExCustomStyle::ControlElement element, const QStyleOption *opt, QPainter *p, const QWidget *w) const; + inline QRect subElementRect(ExCustomStyle::SubElement subElement, const QStyleOption *option, const QWidget *widget) const; + inline int pixelMetric(ExCustomStyle::PixelMetric metric, const QStyleOption *option, const QWidget *widget) const; + inline QSize sizeFromContents(ExCustomStyle::ContentsType ct, const QStyleOption *opt, const QSize &contentsSize, const QWidget *w) const; + inline int styleHint(ExCustomStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const; + inline QIcon standardIcon(ExCustomStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const; +// inline void drawComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const; //这三个不用写成内联函数,且会编译不通过 +// inline QRect subControlRect(ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const; +// inline QStyle::SubControl hitTestComplexControl(ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt, const QWidget *widget) const; //重写QCommonStyle重载的函数 virtual void drawPrimitive(QStyle::PrimitiveElement pe, const QStyleOption *opt, QPainter *p, const QWidget *w) const override; @@ -95,9 +108,21 @@ public: virtual int styleHint(QStyle::StyleHint stylehint, const QStyleOption *opt, const QWidget *widget, QStyleHintReturn *returnData) const override; virtual QIcon standardIcon(QStyle::StandardPixmap standardIcon, const QStyleOption *option, const QWidget *widget) const override; + virtual void drawComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, QPainter *p, const QWidget *widget) const override; + virtual QRect subControlRect(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, SubControl sc, const QWidget *widget) const override; + virtual SubControl hitTestComplexControl(QStyle::ComplexControl cc, const QStyleOptionComplex *opt, const QPoint &pt, const QWidget *widget) const override; + // virtual void polish(QPalette &palette) override; //通常在此函数内指定配色方案,也即配置调色板 // virtual void polish(QWidget *widget) override; //当样式应用到窗口部件时,polish(QWidget*)就会调用,从而允许我们进行最后的定制 // virtual void unpolish(QWidget *widget) override; //当动态改变样式的时候,unpolish就会调用,来撤销polish的影响。polish(QWidget*)一般用做窗口部件的事件过滤器。 + + using QCommonStyle::drawPrimitive; //重实现,使得父类的多个同名 函数 (不同参数) 任然可以在本类里面使用 + using QCommonStyle::drawControl; + using QCommonStyle::subElementRect; + using QCommonStyle::pixelMetric; + using QCommonStyle::sizeFromContents; + using QCommonStyle::styleHint; + using QCommonStyle::standardIcon; }; CUSTOMSTYLE_END_NAMESPACE