Setting color of Tab part of QTabWidget

梦想的初衷 提交于 2019-12-08 11:44:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!