|
如图采用QProxyStyle实现的自定义Tabwidget,现在要怎么改变tabbar的颜色和尺寸呢,我在网上看到了devbean关于这个类的使用的文章,但是看得一知半解,自己试着去改,但是没改成功,文档上说不能使用QSS,这个类的一些函数也看的不明白,请教@devbean 该怎么做,thanks
相关代码:
customtabstye.h- #ifndef CUSTOMTABSTYLE_H
- #define CUSTOMTABSTYLE_H
- #include <QProxyStyle>
- #include <QTabBar>
- class CustomTabStyle : public QProxyStyle
- {
- public:
- explicit CustomTabStyle(Qt::Orientation orientation, QStyle * baseStyle = 0);
- QSize sizeFromContents(ContentsType type,
- const QStyleOption *option,
- const QSize &size,
- const QWidget *widget) const;
- void drawControl(ControlElement element,
- const QStyleOption *option,
- QPainter *painter,
- const QWidget *widget) const;
- QTabBar::Shape determineShape(QTabBar::Shape shape) const;
- private:
- const Qt::Orientation mOrientation;
- };
- #endif // CUSTOMTABSTYLE_H
复制代码 customtabstye.cpp:- #include "customtabstyle.h"
- #include <QPainter>
- #include <QStyleOptionTab>
- CustomTabStyle::CustomTabStyle(Qt::Orientation orientation, QStyle *baseStyle)
- : QProxyStyle(baseStyle), mOrientation(orientation)
- {
- }
- QSize CustomTabStyle::sizeFromContents(QStyle::ContentsType type,
- const QStyleOption *option,
- const QSize &size,
- const QWidget *widget) const
- {
- QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);
- if (type == QStyle::CT_TabBarTab)
- {
- if (const QStyleOptionTab * tab = qstyleoption_cast<const QStyleOptionTab *>(option))
- {
- if (mOrientation == Qt::Horizontal)
- {
- switch (tab->shape)
- {
- case QTabBar::RoundedWest: s.transpose(); break;
- case QTabBar::RoundedEast: s.transpose(); break;
- case QTabBar::TriangularWest: s.transpose(); break;
- case QTabBar::TriangularEast: s.transpose(); break;
- default: break;
- }
- }
- else if (mOrientation == Qt::Vertical)
- {
- switch (tab->shape)
- {
- case QTabBar::RoundedNorth: s.transpose(); break;
- case QTabBar::RoundedSouth: s.transpose(); break;
- case QTabBar::TriangularNorth: s.transpose(); break;
- case QTabBar::TriangularSouth: s.transpose(); break;
- default: break;
- }
- }
- }
- }
- return s;
- }
- void CustomTabStyle::drawControl(QStyle::ControlElement element,
- const QStyleOption *option,
- QPainter *painter,
- const QWidget *widget) const
- {
- if (element == CE_TabBarTabLabel)
- {
- if (const QStyleOptionTab * tab = qstyleoption_cast<const QStyleOptionTab *>(option))
- {
- QStyleOptionTab opt(*tab);
- opt.shape = determineShape(tab->shape);
- return QProxyStyle::drawControl(element, &opt, painter, widget);
- }
- }
- QProxyStyle::drawControl(element, option, painter, widget);
- }
- QTabBar::Shape CustomTabStyle::determineShape(QTabBar::Shape shape) const
- {
- if (mOrientation == Qt::Horizontal)
- {
- switch(shape)
- {
- case QTabBar::RoundedWest: shape = QTabBar::RoundedNorth; break;
- case QTabBar::RoundedEast: shape = QTabBar::RoundedSouth; break;
- case QTabBar::TriangularWest: shape = QTabBar::TriangularNorth; break;
- case QTabBar::TriangularEast: shape = QTabBar::TriangularSouth; break;
- default: break;
- }
- }
- else if (mOrientation == Qt::Vertical)
- {
- switch(shape)
- {
- case QTabBar::RoundedNorth: shape = QTabBar::RoundedWest; break;
- case QTabBar::RoundedSouth: shape = QTabBar::RoundedWest; break;
- case QTabBar::RoundedEast: shape = QTabBar::RoundedWest; break;
- case QTabBar::TriangularNorth: shape = QTabBar::TriangularWest; break;
- case QTabBar::TriangularSouth: shape = QTabBar::TriangularWest; break;
- case QTabBar::TriangularEast: shape = QTabBar::TriangularWest; break;
- default: break;
- }
- }
- return shape;
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|