Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTabBar paintEvent issue



  • Hi! I want to set the tabs to the right direction in my application. I have set it - setTabPosition(QTabWidget::West);. The problem is with the text of the tabs, it is vertical.

    0_1532274824130_2018-07-22_185334.png

    I have sub-classed the QTabBar and re-implement the paintEvent.

    void TestTabBar::paintEvent(QPaintEvent *event)
    {
        QStyleOptionTab tab;
        initStyleOption(&tab, 0);
        tab.text = QString();
        QStylePainter tabPainter(this);
        tabPainter.setRenderHint(QPainter::Antialiasing);
        tabPainter.drawControl(QStyle::CE_TabBarTab, tab);
        tabPainter.setPen(Qt::black);
        tabPainter.drawText(this->tabRect(0), Qt::AlignBottom | Qt::AlignHCenter, this->tabText(0));
        QTabBar::paintEvent(event);
    }
    
    Test::Test(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Test)
    {
         QTabWidget *tabControl = new QTabWidget(this); 
         tabControl->setTabPosition(QTabWidget::West);
         TestTabBar *tabBar1 = new TestTabBar(tabControl);
         TestTabBar *tabBar2 = new TestTabBar(tabControl);
         tabControl->addTab(tabBar1, "Test1");
         tabControl->addTab(tabBar2, "Test2");
         tabControl->setStyleSheet("QTabBar::tab {width: 115px; height: 30px; color: #000000;}");
    }
    

    Any ideas how to display the tabs text horizontally? Thanks.



  • Try adding Qt::Horizontal flag to the drawText function.



  • @AasiApina

    I have set it: tabPainter.drawText(this->tabRect(0), Qt::Horizontal, this->tabText(0)); but it is still vertical:

    0_1532276568176_2018-07-22_192239.png

    I think the problem is with qDebug() << this->tabRect(0).size();, it returns QSize(0, 0). So the tab text could not be drawn at 0 width and height.



  • I have fixed it by this code:

    #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();
    }
    

    Now the problem is solved. Thank you.



  • The following code also works well.

    #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);
            QStyleOptionTabV2 opt;
            
            for(int i = 0;i < count();i++)
            {
                initStyleOption(&opt,i);
                painter.drawControl(QStyle::CE_TabBarTabShape, opt);
                painter.drawItemPixmap(opt.rect, Qt::AlignHCenter|Qt::AlignTop, opt.icon.pixmap(opt.iconSize));
                painter.drawItemText(opt.rect, Qt::AlignHCenter|Qt::AlignBottom, palette(), 1, opt.text);
            }
        }
    };
    
    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.setStyleSheet(QStringLiteral("QTabBar::tab{ min-height: 45px;}"));
        w.resize(600, 400);
        w.addTab(new QWidget, QIcon(qApp->style()->standardIcon(QStyle::SP_MediaPlay)), "tab1");
        w.addTab(new QWidget, QIcon(qApp->style()->standardIcon(QStyle::SP_MediaStop)),"tab2");
        w.addTab(new QWidget, QIcon(qApp->style()->standardIcon(QStyle::SP_MediaPause)),"tab3");
        
        w.show();
    
        return a.exec();
    }
    

Log in to reply