天天看点

QTabWidget 竖向 QTabBar横向endif // CUSTOMTABSTYLE_H

参考文件引用自http://blog.csdn.net/skyztttt/article/details/52448992

你的评论,是我的动力

正常设置QTabWidget->setTabPosition(QTabWidget::West);设置完竖向之后会发现QTabBar仍然是竖向的

所以我们需要重绘一下Qtabbar 下面是重绘代码

/******************customTabStyle.h**************************/

QTabWidget 竖向 QTabBar横向endif // CUSTOMTABSTYLE_H
#ifndef CUSTOMTABSTYLE_H
#define CUSTOMTABSTYLE_H
#include <QPainter>
#include <QProxyStyle>
#include <QStyleOptionTab>
#include <QRect>
#include <QSize>
class CustomTabStyle : 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() = ; // 设置每个tabBar中item的大小
            s.rheight() = ;
        }
        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)) {
                QRect allRect = tab->rect;
                allRect.setWidth(allRect.width() - );
                allRect.setHeight(allRect.height() - );
                //选中状态
                if (tab->state & QStyle::State_Selected) {
                    //save用以保护坐标,restore用来退出状态
                    painter->save();
                    painter->setBrush(QBrush());
                    //矩形
                    //painter->drawRect(allRect.adjusted(0, 0, 0, 0));
                    //带有弧线矩形
                    painter->drawRoundedRect(tab->rect, , );
                    painter->restore();
                }
                //hover状态
                else if(tab->state & QStyle::State_MouseOver){
                    painter->save();
                    painter->setBrush(QBrush());
                    painter->drawRoundedRect(allRect, , );
                    painter->restore();
                }
                else{
                    painter->save();
                    painter->setBrush(QBrush());
                    painter->drawRoundedRect(allRect, , );
                    painter->restore();
                }
                QTextOption option;
                option.setAlignment(Qt::AlignCenter);
                painter->setFont(QFont("楷体", , QFont::Bold));
                painter->setPen();
                painter->drawText(allRect, tab->text, option);
                return;
            }
        }
        if (element == CE_TabBarTab) {
            QProxyStyle::drawControl(element, option, painter, widget);
        }
    }
};
           

endif // CUSTOMTABSTYLE_H

/****************************引用方法*****************************/

QTabWidget* tab = new QTabWidget();
    QPushButton* closeButton = new QPushButton;
    closeButton->setObjectName("closeButton");
    QPushButton* button = new QPushButton("button");
    tab->addTab(closeButton, "关闭");
    tab->addTab(button, "按钮");
    tab->setTabPosition(QTabWidget::West);//QTabWidget竖向
    tab->tabBar()->setStyle(new CustomTabStyle);//注意,设置上述代码风格 就可以实现QTabBar横向
    setCentralWidget(tab);
           

继续阅读