找回密码
 立即注册
收起左侧

如何更改Tabbar的颜色和尺寸

0
回复
8407
查看
[复制链接]
累计签到:882 天
连续签到:3 天
来源: 2015-3-29 18:11:12 显示全部楼层 |阅读模式
如图采用QProxyStyle实现的自定义Tabwidget,现在要怎么改变tabbar的颜色和尺寸呢,我在网上看到了devbean关于这个类的使用的文章,但是看得一知半解,自己试着去改,但是没改成功,文档上说不能使用QSS,这个类的一些函数也看的不明白,请教@devbean 该怎么做,thanks

相关代码:
customtabstye.h
  1. #ifndef CUSTOMTABSTYLE_H
  2. #define CUSTOMTABSTYLE_H

  3. #include <QProxyStyle>
  4. #include <QTabBar>

  5. class CustomTabStyle : public QProxyStyle
  6. {
  7. public:

  8.     explicit CustomTabStyle(Qt::Orientation orientation, QStyle * baseStyle = 0);

  9.     QSize sizeFromContents(ContentsType type,
  10.                            const QStyleOption *option,
  11.                            const QSize &size,
  12.                            const QWidget *widget) const;
  13.     void drawControl(ControlElement element,
  14.                      const QStyleOption *option,
  15.                      QPainter *painter,
  16.                      const QWidget *widget) const;
  17.     QTabBar::Shape determineShape(QTabBar::Shape shape) const;

  18. private:
  19.     const Qt::Orientation mOrientation;
  20. };

  21. #endif // CUSTOMTABSTYLE_H
复制代码
customtabstye.cpp:
  1. #include "customtabstyle.h"
  2. #include <QPainter>
  3. #include <QStyleOptionTab>

  4. CustomTabStyle::CustomTabStyle(Qt::Orientation orientation, QStyle *baseStyle)
  5.     : QProxyStyle(baseStyle), mOrientation(orientation)
  6. {

  7. }

  8. QSize CustomTabStyle::sizeFromContents(QStyle::ContentsType type,
  9.                                        const QStyleOption *option,
  10.                                        const QSize &size,
  11.                                        const QWidget *widget) const
  12. {
  13.     QSize s = QProxyStyle::sizeFromContents(type, option, size, widget);

  14.     if (type == QStyle::CT_TabBarTab)
  15.     {
  16.         if (const QStyleOptionTab * tab = qstyleoption_cast<const QStyleOptionTab *>(option))
  17.         {
  18.             if (mOrientation == Qt::Horizontal)
  19.             {
  20.                 switch (tab->shape)
  21.                 {
  22.                     case QTabBar::RoundedWest:      s.transpose();  break;
  23.                     case QTabBar::RoundedEast:      s.transpose();  break;
  24.                     case QTabBar::TriangularWest:   s.transpose();  break;
  25.                     case QTabBar::TriangularEast:   s.transpose();  break;
  26.                     default:                                        break;
  27.                 }
  28.             }
  29.             else if (mOrientation == Qt::Vertical)
  30.             {
  31.                 switch (tab->shape)
  32.                 {
  33.                     case QTabBar::RoundedNorth:     s.transpose();  break;
  34.                     case QTabBar::RoundedSouth:     s.transpose();  break;
  35.                     case QTabBar::TriangularNorth:  s.transpose();  break;
  36.                     case QTabBar::TriangularSouth:  s.transpose();  break;
  37.                     default:                                        break;
  38.                 }
  39.             }
  40.         }
  41.     }

  42.     return s;
  43. }

  44. void CustomTabStyle::drawControl(QStyle::ControlElement element,
  45.                                  const QStyleOption *option,
  46.                                  QPainter *painter,
  47.                                  const QWidget *widget) const
  48. {
  49.     if (element == CE_TabBarTabLabel)
  50.     {
  51.         if (const QStyleOptionTab * tab = qstyleoption_cast<const QStyleOptionTab *>(option))
  52.         {
  53.             QStyleOptionTab opt(*tab);
  54.             opt.shape = determineShape(tab->shape);
  55.             return QProxyStyle::drawControl(element, &opt, painter, widget);
  56.         }
  57.     }
  58.     QProxyStyle::drawControl(element, option, painter, widget);
  59. }

  60. QTabBar::Shape CustomTabStyle::determineShape(QTabBar::Shape shape) const
  61. {
  62.     if (mOrientation == Qt::Horizontal)
  63.     {
  64.         switch(shape)
  65.         {
  66.             case QTabBar::RoundedWest:      shape = QTabBar::RoundedNorth;      break;
  67.             case QTabBar::RoundedEast:      shape = QTabBar::RoundedSouth;      break;
  68.             case QTabBar::TriangularWest:   shape = QTabBar::TriangularNorth;   break;
  69.             case QTabBar::TriangularEast:   shape = QTabBar::TriangularSouth;   break;
  70.             default:                                                            break;
  71.         }
  72.     }
  73.     else if (mOrientation == Qt::Vertical)
  74.     {
  75.         switch(shape)
  76.         {
  77.             case QTabBar::RoundedNorth:     shape = QTabBar::RoundedWest;       break;
  78.             case QTabBar::RoundedSouth:     shape = QTabBar::RoundedWest;       break;
  79.             case QTabBar::RoundedEast:      shape = QTabBar::RoundedWest;       break;
  80.             case QTabBar::TriangularNorth:  shape = QTabBar::TriangularWest;    break;
  81.             case QTabBar::TriangularSouth:  shape = QTabBar::TriangularWest;    break;
  82.             case QTabBar::TriangularEast:   shape = QTabBar::TriangularWest;    break;
  83.             default:                                                            break;
  84.         }
  85.     }
  86.     return shape;
  87. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了