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. Change font of QTabWidget
Forum Updated to NodeBB v4.3 + New Features

Change font of QTabWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.4k 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.
  • krzysieklfcK Offline
    krzysieklfcK Offline
    krzysieklfc
    wrote on last edited by
    #1

    I read that QTabWidget uses OS specific styling and it may be tricky to change the look. I experimented a lot but still can't figure it out.
    I subclassed both QTabWidget and QTabBar and set the custom TabBar.

    void TabBar::tabBarPaintEvent(QPaintEvent* event)
    {
    	QStylePainter painter(this);
    	QStyleOptionTab opt;
    
    	for (int i = 0; i != count(); i++)
    	{
    		initStyleOption(&opt, i);
    		painter.setFont(QFont("Roboto", 15));
    		painter.drawControl(QStyle::CE_TabBarTabShape, opt);
    		painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
    	}
    }
    
    
    
    KillerSmathK 1 Reply Last reply
    0
    • krzysieklfcK krzysieklfc

      I read that QTabWidget uses OS specific styling and it may be tricky to change the look. I experimented a lot but still can't figure it out.
      I subclassed both QTabWidget and QTabBar and set the custom TabBar.

      void TabBar::tabBarPaintEvent(QPaintEvent* event)
      {
      	QStylePainter painter(this);
      	QStyleOptionTab opt;
      
      	for (int i = 0; i != count(); i++)
      	{
      		initStyleOption(&opt, i);
      		painter.setFont(QFont("Roboto", 15));
      		painter.drawControl(QStyle::CE_TabBarTabShape, opt);
      		painter.drawControl(QStyle::CE_TabBarTabLabel, opt);
      	}
      }
      
      
      
      KillerSmathK Offline
      KillerSmathK Offline
      KillerSmath
      wrote on last edited by
      #2

      Hi @krzysieklfc and welcome to Qt Forum.

      The QTabBar uses a default style to paint itself.
      You can change the font family, font size, background color and several properties using a QProxyStyle to change how the QTabBar is painted.

      #include <QProxyStyle>
      #include <QPainter>
      
      class TabBarProxyStyle : public QProxyStyle {
      public:
          TabBarProxyStyle(QStyle *style = nullptr) : QProxyStyle(style) { }
      
          void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override
          {
              if (element == CE_TabBarTab) {
                  painter->save(); // save the painter to restore later
                  painter->setFont(QFont("Roboto", 15)); // change the defaul font of painter
                  QProxyStyle::drawControl(element, option, painter, widget); // paint the TabBarTab using the default drawControl
                  painter->restore(); // restore to default painter
              }
              else{
                  QProxyStyle::drawControl(element, option, painter, widget); // use default drawControl to paint all other components without changes.
              }
          }
      };
      

      So, you just need to change the default style to the custom ProxyStyle

      tabWidget->tabBar()->setStyle(new TabBarProxyStyle);
      

      @Computer Science Student - Brazil
      Web Developer and Researcher
      “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

      1 Reply Last reply
      4

      • Login

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