How can I put a background image in the first tab of a QTabWidget?
-
@Igor86
So try putting your style sheet onto theQWidget
you place as the first tab, not on theQTabWidget
itself.Your path is a "resource path" because it begins with
:/
. Setting up resources for a Python Qt is not so easy as you do not build an executable. It can be done if you want, but you would have to read how. If you are happy to do this by an external file instead, specify the full path to that file (/...
, but not:/...
). -
@Igor86
And the full path to your file which I stressed you will need is the/test.png
that you have written there? I very much doubt it....!Get the path working first for a
QImage
or aQLabel
orQPushButton
with image or similar, so that you know it works. Then try it with theQWidget
. -
thank you, I got it working now..
but somehting is not entirely right:
i applied it to the "tab" widget, but also 2 QLabels contained in the Qwidget got the same background..
i'd expect the two squares to be either empty, or transparent..
(i did it via code, not in the designer, like this:
self.tabWidget.findChild(QWidget, "tab").setStyleSheet("background-image: url(\"" + os.path.dirname(__file__) + "/test.png\");")
-
@Igor86
I guess unqualifiedbackground-image: url(...)
applies the background image to all descendants of theQWidget
.Try
QWidget { background-image: url(...); }
on it. Does that restrict it to yourQWidget
, or does it still inherit toQLabel
s because they are derived fromQWidget
?If the latter try
#FirstPage { background-image: url(...); }
and set yourQWidget
's object name toFirstPage
, which you can do at the top of the widget's properties you show in Designer. -
@Igor86
Good. If you had set its object tab totab
per your code you could obviously have used that for its name/selector, but I thinkFirstTab
is clearer.You might find that if you don't want to give it an object name
.QWidget { background-image: url(...); }
might have worked. That has a.
(dot) before the widget class name. Per https://doc.qt.io/qt-5/stylesheet-syntax.html#selector-types (which you might want to read for these stylesheet things)Class Selector .QPushButton Matches instances of QPushButton, but not of its subclasses.
This is equivalent to *[class~="QPushButton"].
But then if you did add any further
QWidget
s onto that page they would be affected.