0

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?

user2494298
  • 299
  • 3
  • 7
  • 23

1 Answers1

3

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
Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147
  • Are you supposed to put this definition outside of a class? When I use this code it doesn't compile. Doesn't seem to like th qlineargradient line – user2494298 Feb 11 '14 at 17:35
  • Stylesheets are present in your code as a string unless referenced externally. I've updated my answer to clarify. – Kaleb Pederson Feb 11 '14 at 20:04