问题
When I change the background color of the QTabWidget, the tab part of the widget doesn't change colors. Looking online there doesn't seem to be a simple way to set this color. Suggestions?
回答1:
You can do that using Qt's style sheets. From the docs:
/* Style the tab using the tab sub-control. Note that
it reads QTabBar _not_ QTabWidget */
QTabBar::tab {
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);
border: 2px solid #C4C4C3;
border-bottom-color: #C2C7CB; /* same as the pane color */
border-top-left-radius: 4px;
border-top-right-radius: 4px;
min-width: 8ex;
padding: 2px;
}
Once you've defined the QSS of interest as a string, you then set the QSS on the widget in question using the setStyleSheet method. If you're only interested in setting the background color, a simple stylesheet will probably suffice:
yourQTabWidget->setStyleSheet("QTabBar::tab { background-color: #FF0000; }");
A -stylesheet
command line option is also available and can be used to style the application. In a well-behaving application, you should then be able to do the following:
yourQtProgram.exe -stylesheet /path/to/your/stylesheet.qss
来源:https://stackoverflow.com/questions/21686998/setting-color-of-tab-part-of-qtabwidget