QT中用Qwidget的QtabWidget使用风格是可以自己定义的下面是一个风格的头文件:
/* / */
/*!
@file CustomTabStyle.h
@author chen
@date 2017/03
@brief The file CustomTabStyle class
@par History
@verbatim
<author> <time> <version> <desc>
chenqiangling 2017/06 0.1.0 build this module
@endverbatim
*/
/* / */
/*!
@addtogroup namespace cqltbe
@{
*/
/*!
@addtogroup common
@{
*/
/*!
@addtogroup common_Exported_Classes
@{
*/
#ifndef CUSTOMTABSTYLE_H
#define CUSTOMTABSTYLE_H
#include <QProxyStyle>
#include <QPainter>
class CustomTabStyle : public QProxyStyle//继承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() = 140; // 设置每个tabBar中item的大小
tabsize.rheight() = 80;//设置高度
}
return tabsize;
}
//重写函数drawControl,绘图控制
//ControlElement控制元件,option,QStyle选项,painter绘图,widget对象
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
if (element == CE_TabBarTabLabel) {//如果元件是TabBarTabLabel
if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(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);
}
}
};
class CustomTabStyle1 : public QProxyStyle//继承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() = 240; // 设置每个tabBar中item的大小
tabsize.rheight() = 38;//设置高度
}
return tabsize;
}
//重写函数drawControl,绘图控制
//ControlElement控制元件,option,QStyle选项,painter绘图,widget对象
void drawControl(ControlElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
if (element == CE_TabBarTabLabel) {//如果元件是TabBarTabLabel
if (const QStyleOptionTab *tab = qstyleoption_cast<const QStyleOptionTab *>(option)) {
QRect allRect = tab->rect;//获取tab矩形框
if (tab->state & QStyle::State_Selected) {
painter->save();
painter->setPen(0x555555);//设置颜色
painter->setBrush(QBrush(0x282828));//设置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);
}
}
};
class CustomTabStyle2 : public QProxyStyle
{
public:
QSize sizeFromContents(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)
{ s.transpose();
s.rwidth() = 240; // 设置每个tabBar中item的大小
s.rheight() = 38;//设置高度
}
return s;
}
void drawControl(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 = QTabBar::RoundedNorth;
//QIcon icon(":/Resources/icon2.ico");
//opt.icon = icon;
opt.palette.setCurrentColorGroup(QPalette::Disabled);
opt.state |= QStyle::State_Sunken;
QProxyStyle::drawControl(element, &opt, painter, widget);
return;
}
}
if (element == CE_TabBarTab)
{
// painter->drawText(allRect, tab->text, option);//绘制文本
//painter->setBackground(QBrush(Qt::red, Qt::SolidPattern));
}
QProxyStyle::drawControl(element, option, painter, widget);
}
};
/*!
@}
*/
/*!
@}
*/
/*!
@}
*/
#endif // CUSTOMTABSTYLE_H
QTabBar *tabStyle=ui->tabWidget->tabBar(); tabStyle->setStyle(new CustomTabStyle);