I want to change a Button's background color and text color without changing/resetting the whole style.
Currently, I'm using this:
dl_btt.setStyleSheet("background-color: green; color: white")
But that changes the whole style of the button:
Since the default style looks like this:
I would like to have something like this:
So, if I were to change OS, I want it to use default style and only change the background color.
Can I do this without manually replicating the style?
EDIT:
@Controlix Thank you for pointing me in the right direction. However, I can't seem to be able to change the background color.
I'm only able to change the border and text color using:
dl_btt = DownloadButton(self, "Skini")
#dl_btt.setStyleSheet("background-color: green; color: white")
#dl_btt.setPalette(QtGui.QPalette(QtCore.Qt.green))
palette = dl_btt.palette()
role = dl_btt.foregroundRole()
palette.setColor(role, QtGui.QColor('white'))
role = dl_btt.backgroundRole()
palette.setColor(role, QtGui.QColor('green'))
dl_btt.setAutoFillBackground(True)
dl_btt.setPalette(palette)
Which gives me this result:
Searching gave me same or similar snippets of code that don't do what I expected it to do.
EDIT 2:
I gave up on this search and used style sheets, rebuilding the styles as I see fit.
I'm still wondering though... Is it possible, in PyQt, to replicate native style while changing only certain part of a widget?
EDIT 3
I tried:
palette = dl_btt.palette()
role = dl_btt.foregroundRole()
palette.setColor(role, QtGui.QColor('white'))
role = dl_btt.buttonRole()
palette.setColor(role, QtGui.QColor('green'))
dl_btt.setAutoFillBackground(True)
dl_btt.setPalette(palette)
But I got this error:
AttributeError: 'DownloadButton' object has no attribute 'buttonRole'
How can I access the button role? How is it called?
Just in case you need it, here is the DownloadButton class:
class DownloadButton(QtGui.QPushButton):
def __init__(self, master, *args, **kwargs):
super(DownloadButton, self).__init__(*args, **kwargs)
self.master = master
def mousePressEvent(self, ev):
self.master.startDownload()