如何在QT中实现垂直制表符? [英] How to implement vertical tabs in QT?

查看:696
本文介绍了如何在QT中实现垂直制表符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用QT实现带有水平文本的垂直选项卡,但是在 QTabWidget 中找不到任何类似的选项.

I am trying to implement vertical tabs with horizontal text with QT but I cannot find any similar option in QTabWidget.

SO中的某人要求在此处 ,但是,答案中包含断开的链接,我怀疑它们是否提供了真正的解决方案.

Somebody in SO asked for something similar here, however, the answers contain broken links and I doubt that they present a real solution.

有人能做到吗?

推荐答案

您必须实现一个自定义的QTabBar,以覆盖tabSizeHint()paintEvent()方法,如下所示:

You have to implement a custom QTabBar overwriting the tabSizeHint() and paintEvent() methods as shown below:

#include <QApplication>
#include <QStyleOptionTab>
#include <QStylePainter>
#include <QTabBar>
#include <QTabWidget>

class TabBar: public QTabBar{
public:
    QSize tabSizeHint(int index) const{
        QSize s = QTabBar::tabSizeHint(index);
        s.transpose();
        return s;
    }
protected:
    void paintEvent(QPaintEvent * /*event*/){
        QStylePainter painter(this);
        QStyleOptionTab opt;

        for(int i = 0;i < count();i++)
        {
            initStyleOption(&opt,i);
            painter.drawControl(QStyle::CE_TabBarTabShape, opt);
            painter.save();

            QSize s = opt.rect.size();
            s.transpose();
            QRect r(QPoint(), s);
            r.moveCenter(opt.rect.center());
            opt.rect = r;

            QPoint c = tabRect(i).center();
            painter.translate(c);
            painter.rotate(90);
            painter.translate(-c);
            painter.drawControl(QStyle::CE_TabBarTabLabel,opt);
            painter.restore();
        }
    }
};

class TabWidget : public QTabWidget
{
public:
    TabWidget(QWidget *parent=0):QTabWidget(parent){
        setTabBar(new TabBar);
        setTabPosition(QTabWidget::West);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TabWidget w;
    w.addTab(new QWidget, "tab1");
    w.addTab(new QWidget, "tab2");
    w.addTab(new QWidget, "tab3");
    w.show();

    return a.exec();
}

这篇关于如何在QT中实现垂直制表符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆