Search code examples
c++qtqtabwidgetqtabbar

Customize appearance QTabWidget


I am currently working on Qt application. There I am using QTabWidget with several tabs.

QTabWidget is embedded into QVBoxLayout. My question is that tabs added appear centered and I would like them to be left aligned. How could I achieve that?

enter image description here

I would like to change appearance as well. Is the only way to override the paintEvent of QTabBar?

I am using Qt and C++, no QML

Thanks and regards


Solution

  • You should try styling the tab widget with a style sheet, this way:

    QString ss = "QTabWidget::tab-bar { alignment: left; }";
    ui->tabWidget->setStyleSheet(ss);
    

    The style sheet can be set using the designer, though. Just right click the widget and choose Change stylesheet ... from the pop up menu, then insert the style in the dialog and click the Ok button when done, or the Apply button to check the effect before closing the dialog box.

    You can find the official documentation here and here.

    You can refer to this post to see how to style the tab widget further.