From f9da06bbcc9d5727673145e6339c892e1422bc53 Mon Sep 17 00:00:00 2001 From: TurnMeOn Date: Thu, 2 Nov 2017 13:37:17 +0800 Subject: [PATCH] tabwidget --- maintabstyle.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ maintabstyle.h | 43 ++-------------------------------------- widget.h | 1 - 3 files changed, 53 insertions(+), 42 deletions(-) diff --git a/maintabstyle.cpp b/maintabstyle.cpp index 8e623a1..69ff59a 100644 --- a/maintabstyle.cpp +++ b/maintabstyle.cpp @@ -1,2 +1,53 @@ #include "maintabstyle.h" + +QSize MainTabStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &size, const QWidget *widget) const +{ + + QSize tabsize = QProxyStyle::sizeFromContents(type, option, size, widget); + if (type == QStyle::CT_TabBarTab) + { + tabsize.transpose(); + tabsize.rwidth() = 90; // 设置每个tabBar中item的大小 + tabsize.rheight() = 44;//设置高度 + } + return tabsize; + +} + +void MainTabStyle::drawControl(QStyle::ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const + +{ + if (element == CE_TabBarTabLabel) + {//如果元件是TabBarTabLabel + if (const QStyleOptionTab *tab = qstyleoption_cast(option)) + { + QRect allRect = tab->rect;//获取tab矩形框 + if (tab->state & QStyle::State_Selected) + { + painter->save(); + painter->setPen(0x89cfff);//设置颜色 + painter->setBrush(QBrush(0x89cfff));//设置Brush颜色 + painter->drawRect(allRect.adjusted(6, 6, 6, -6));//这里调整高亮 + painter->restore();//恢复 + } + QTextOption option;//这里设置文本样式 + option.setAlignment(Qt::AlignCenter);//设置对其方式。居中对齐 + if (tab->state & QStyle::State_Selected) + { + painter->setPen(0xf8fcff);//设置颜色 + } + else + { + painter->setPen(0x5d5d5d); + } + painter->drawText(allRect, tab->text, option);//绘制文本 + return; + } + } + if (element == CE_TabBarTab) + { + QProxyStyle::drawControl(element, option, painter, widget); + } +} + diff --git a/maintabstyle.h b/maintabstyle.h index 1623797..c25dcd9 100644 --- a/maintabstyle.h +++ b/maintabstyle.h @@ -10,46 +10,7 @@ class MainTabStyle:public QProxyStyle { public: QSize sizeFromContents(ContentsType type, const QStyleOption *option, - const QSize &size, const QWidget *widget) const - { - QSize tabsize = QProxyStyle::sizeFromContents(type, option, size, widget); - if (type == QStyle::CT_TabBarTab) { - tabsize.transpose(); - tabsize.rwidth() = 90; // 设置每个tabBar中item的大小 - tabsize.rheight() = 44;//设置高度 - } - return tabsize; - } - void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const - { - if (element == CE_TabBarTabLabel) - {//如果元件是TabBarTabLabel - if (const QStyleOptionTab *tab = qstyleoption_cast(option)) { - QRect allRect = tab->rect;//获取tab矩形框 - if (tab->state & QStyle::State_Selected) { - painter->save(); - painter->setPen(0x89cfff);//设置颜色 - painter->setBrush(QBrush(0x89cfff));//设置Brush颜色 - painter->drawRect(allRect.adjusted(6, 6, 6, -6));//这里调整高亮 - painter->restore();//恢复 - } - QTextOption option;//这里设置文本样式 - option.setAlignment(Qt::AlignCenter);//设置对其方式。居中对齐 - if (tab->state & QStyle::State_Selected) { - painter->setPen(0xf8fcff);//设置颜色 - } - else { - painter->setPen(0x5d5d5d); - } - painter->drawText(allRect, tab->text, option);//绘制文本 - return; - } - } - if (element == CE_TabBarTab) { - QProxyStyle::drawControl(element, option, painter, widget); - - } - } + const QSize &size, const QWidget *widget) const; + void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const; }; - #endif // MAINTABSTYLE_H diff --git a/widget.h b/widget.h index d238e3d..2fc81a1 100644 --- a/widget.h +++ b/widget.h @@ -6,7 +6,6 @@ #include #include #include -#include namespace Ui { class Widget;