how to create a subclass to rotate a text in a tabbar of tab widget



  • : error: 'QTabBar* QTabWidget::tabBar() const' is protected

    i tried,

    in cpp file//

     ui->setupUi(this);
     ui->fptabwidget->setStyleSheet("QTabBar::tab{height:160px;width:64}");
        ui->fptabwidget->tabBar()->setStyle(new CustomTabStyle);
    }
    
    

    in header file //

    #include <QProxyStyle>
    #include <QStyleOption>
    #include <QTabBar>
    
    
    namespace Ui {
    class firstpage;
    }
    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();
        }
        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::RoundedWest;
            QProxyStyle::drawControl(element, &opt, painter, widget);
            return;
          }
        }
        QProxyStyle::drawControl(element, option, painter, widget);
      }
    };
    

    i have used the proxy style but i dont know how to rotate the text
    the above first line is the error ,

    I think this is cleared by creating a subclass and do the followng code
    but,i am not well clear about the creating the sub class .
    How to do this?

    Thanks in Advance ,....


  • Qt Champions 2017

    Hi
    It seems its protected in Qt4.
    So you will need to subclass and surface tabBar()

    class MyTabWidget : public QTabWidget {
      Q_OBJECT
    public:
      explicit MyTabWidget(QWidget* parent = Q_NULLPTR) : QTabWidget(parent)  {}
      QTabBar *GetTabBar() { return tabBar(); }
    };
    
    
    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();
        }
        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;
            QProxyStyle::drawControl(element, &opt, painter, widget);
            return;
          }
        }
        QProxyStyle::drawControl(element, option, painter, widget);
      }
    };
    

    then use promotion to replace with "MyTabWidget"
    http://doc.qt.io/qt-5/designer-using-custom-widgets.html

    and then apply it
    ui->tabWidget->GetTabBar()->setStyle(new CustomTabStyle);

    and hopefully it works the same in older Qt versions.



  • @mrjj thanks a lot



  • @mrjj bro i got a another error !!

    error: 'Q_NULLPTR' was not declared in this scope



  • @sankarapandiyan a quick google search would have lead you to find the simple "replace Q_NULLPTR with NULL" answer



  • @VRonin oh !!,So we are using Null instead of Q_NULPTR
    thanks VRonin


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.