0

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":

enter image description here

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?

4

1 回答 1

2

我将问题解释为:

如何避免将用于一种小部件类型的样式与另一种重叠?

请注意:

  • 样式表从父小部件传播到子小部件。
  • 样式表具有您可以为类型显式设置的可见性。


    myObj.setStyleSheet("QPushButton {border: 0.05em solid lightgray;}" )

只需将样式表与例如QType { ... }.

我只处理 C++ Qt,但认为 PyQt 使用相同的样式表系统。

于 2016-04-20T15:17:13.580 回答