The style of each individual QTabWidget tab is different

I have a QTabWidget that contains 4 tabs. I would like to create each title for each one separately: I saw that I could use a stylesheet to do this. But my problem is that I could not just change the title of one tab, where the name of the tab is, without changing the rest of the tab.

In a simple way, imagine that I want the first tab to be red, the second blue, 3rd green and 4th yellow. So, how can I change the style of each individual tab without changing the others.

Thanks!

EDIT

I saw there how I could immediately change the style of all tab headers, but not individually

+4
source share
1 answer

If you subclass QTabWidget, you can access the protected function QTabWidget::tabBar() , which returns the QTabBar used. QTabBar has a QTabBar::setTabTextColor() method that will change the text color of an individual tab. Here is an example:

 #include <QtGui> class TabWidget : public QTabWidget { public: TabWidget() { addTab(new QPushButton("Hi 1!"), "Button 1 Tab"); addTab(new QPushButton("Hi 2!"), "Button 2 Tab"); tabBar()->setTabTextColor(0, QColor(Qt::red)); tabBar()->setTabTextColor(1, QColor(Qt::blue)); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); TabWidget tabWidget; tabWidget.show(); return app.exec(); } 

If you need more control, you can create your own tab widget. According to docs , a QTabWidget is basically a QStackedWidget combined with a QTabBar. You can create your own tab widget by combining a QStackedWidget with, for example, a set of stylized QPushButtons.

+5
source

Source: https://habr.com/ru/post/1414193/


All Articles