Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTabBar paintEvent issue
Qt 6.11 is out! See what's new in the release blog

QTabBar paintEvent issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 3.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AasiApina
      wrote on last edited by
      #2

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

      Cobra91151C 1 Reply Last reply
      0
      • A AasiApina

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

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by Cobra91151
        #3

        @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.

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SkyBoy
            wrote on last edited by
            #5

            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();
            }
            
            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved