How can i change the background of a single tab?
-
Hi all!
hope this is the correct forum..
i am facing some issues in trying to change the background color of a single tab in pyqt6.
my code is this:
self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
any help is greatly appreciated!
-
Hi all!
hope this is the correct forum..
i am facing some issues in trying to change the background color of a single tab in pyqt6.
my code is this:
self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
any help is greatly appreciated!
-
@JonB the tab stays the same, no red background. what i need is just the tab header/title to have a red background. not the page. the code above has no effect at all.. the tab exists, and the child is also found...
@Igor86
And exactly what widget have you set and retrieved asmyTab
? We don't know!I believe you need to be looking in a
QTabBar
for your "title" tabs. I think setTabTextColor(index, color) will change an individual tab's title/text color. From a stylesheet you probably need to setQTabBar::tab { background-color: red; }
. That would set color on all tabs, I don't know about an individual one from stylesheet. -
@Igor86
And exactly what widget have you set and retrieved asmyTab
? We don't know!I believe you need to be looking in a
QTabBar
for your "title" tabs. I think setTabTextColor(index, color) will change an individual tab's title/text color. From a stylesheet you probably need to setQTabBar::tab { background-color: red; }
. That would set color on all tabs, I don't know about an individual one from stylesheet.@JonB said in How can i change the background of a single tab?:
And exactly what widget have you set and retrieved as
myTab
? We don't know!self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
self.tabWidget is a QTabWidget.
it finds the child with the name "myTab" and that is a Tab. Unfortunately changing the text is not enough for me, as i need to change the background. and youre right, the stylesheet applies to all tabs, not just one.. -
@JonB said in How can i change the background of a single tab?:
And exactly what widget have you set and retrieved as
myTab
? We don't know!self.tabWidget.findChild(QtWidgets.QWidget, "myTab").setStyleSheet("background-color: red;")
self.tabWidget is a QTabWidget.
it finds the child with the name "myTab" and that is a Tab. Unfortunately changing the text is not enough for me, as i need to change the background. and youre right, the stylesheet applies to all tabs, not just one..@Igor86
So as I understand itQTabBar
has methods to set individual tab text (foreground) color but not background. And stylesheetQTabBar::tab { background-color: red; }
does what you want but sets all tabs.Read through the (very long) QTabWidget, setting colour of tab itself. I did not digest it all, there may be approaches there. @J.Hilk's "cheaty"
QTabBar::tab [whatsThis="aTabWithErrors"]{
may work but leveraging thewhatsThis
property. There are various other possibilities discussed there, I will leave you to investigate them.Another possibility: the
QTabBar
you can access fromQTabWidget
hastabButton()
&setTabButton()
methods to access/set aQWidget
for each tab. Assuming that is the widget for the "tab" itself you could presumably access the existing one or replace with your own where you can set its background color, either from stylesheet or code?https://forum.qt.io/topic/59018/background-color-of-a-specific-tab-of-qtabbar-qtabwidget also seems to discuss this. And the final post at https://forum.qt.io/post/822309 by @Dariusz claims to be working Python code overriding
def paintEvent()
to achieve just what you want?