How to add an icon to a QTabWidget (not for each t

2019-09-19 16:05发布

问题:

I would like to add an icon at the top left of the QTabWidget. I don't want to add an icon for each tab. How to do that using a stylesheet or programmatically in C++?

回答1:

Stylesheet below will add an image to the left of all tabs, but not to individual tabs. However, it is very likely that you'll need to tweak other attributes in the CSS to polish the final result.

QTabWidget > QTabBar {
  background: url(url-to-your-corner-image);
  background-repeat: norepeat;
  margin-top: 10px; /* whatever needed to center the image vertically */
}
QTabWidget > QTabBar::tab:first {
  margin-left: 10px; /* at least the width of your image */
}


回答2:

You can add a first tab with a fixed stylesheet : TabWidget stylesheet example