I'm having a problem with setting the border-image
property of a QToolButton
(later on also of a QPushButton
but I assume the problem will occur there too). Each of my custom buttons (inheriting from QToolButton
and QPushButton
respectively) has to have 3 states - inactive, running and error.
Inactive:
style = 'border-top-right-radius: ' + str(self.cornerRadius) + 'px; border-bottom-right-radius: ' + str(self.cornerRadius) + 'px; border-top-left-radius: ' + str(self.cornerRadius) + 'px; border-bottom-left-radius: ' + str(self.cornerRadius) + 'px; border-image: url("' + self.icon + '"); background: none;'
Running:
style = 'border-top-right-radius: ' + str(self.cornerRadius) + 'px; border-bottom-right-radius: ' + str(self.cornerRadius) + 'px; border-top-left-radius: ' + str(self.cornerRadius) + 'px; border-bottom-left-radius: ' + str(self.cornerRadius) + 'px; border-image: url("' + self.icon + '"); background: rgb(89, 205, 139);'
Error:
style = 'border-top-right-radius: ' + str(self.cornerRadius) + 'px; border-bottom-right-radius: ' + str(self.cornerRadius) + 'px; border-top-left-radius: ' + str(self.cornerRadius) + 'px; border-bottom-left-radius: ' + str(self.cornerRadius) + 'px; border-image: url("' + self.icon + '"); background: rgb(215, 56, 56);'
where self.cornerRadius
is some number used to determine the roundness of my buttons in pixels and self.icon
is an icon I have retrieved from somewhere.
All I do in my button's constructor (among other simple initializations) is
self.setStyleSheet(style)
The clicked()
signal of the button is connected to a slot in the same class where the state is changed (along with the style using self.setStyleSheet(...)
) in respect to what the system's new state is.
For some unknown reason my tooltip receives the style changes of the button it "belongs to":
I've double-checked the Qt documentation and I see no reason that will explain this behaviour. I do NOT want to change the tooltip's style at all. Any ideas how to fix this?
I interpret the question as:
How to avoid overlapping the style intended for one widget type with another?
Mind that:
The stylesheet has visibility you can explicitly set for the type.
myObj.setStyleSheet("QPushButton {border: 0.05em solid lightgray;}" )
Just enclose the stylesheet with the type e.g. QType { ... }
.
I deal only with C++ Qt but think that PyQt uses same stylesheet system.