问题
I want the border of my QTWidgets.QWidget to be transparent or disabled (borderwidth=0), but when I set it via the stylesheet to "border:0", also all the push buttons inside the widget dont have a border - any idea how to keep the style of the pushbuttons and only change the widget border ?
class lamp_frame(object):
def setupUi(self, window):
window.setObjectName("Yeelight Controlle")
window.resize(460, 140+self.window_hight)
window.setFixedSize(460, 140+self.window_hight)
self.frame_0 = QtWidgets.QFrame(window)
self.frame_0.setGeometry(QtCore.QRect(20, 30+self.top, 420, 70))
self.frame_0.setStyleSheet("border:0")
self.frame_0.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_0.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_0.setObjectName("frame_0")
self.pushButton_0 = QtWidgets.QPushButton(self.frame_0)
self.pushButton_0.setGeometry(QtCore.QRect(30, 35, 50, 32))
self.pushButton_0.setObjectName("status")
回答1:
Stylesheets are, by definition, cascading. From the wikipedia description:
The style sheet with the highest priority controls the content display. Declarations not set in the highest priority source are passed on to a source of lower priority
This means that if a "generic" property is set on an object without specifying the "target" in any way, every child object will inherit that property. Since the buttons are children of the frame, their border will be inherited by it.
It's common convention to specify a selector for the target of the properties, in order to correctly apply those properties only to the selected objects.
This means that using a simple "border: 0;" will result in using that border not only on the frame, but also to all its children, and that's why these kind of "generic stylesheets" should be avoided, even if they work.
Qt selectors are listed in the syntax documentation, and in your case you have two options:
- set the stylesheet for the object class:
QFrame { border: 0; }
- use the object name:
#frame_0 { border: 0; }
Obviously, selectors can be mixed according to the needs. To be more specific, since theoretically more object could have the same object name (but Designer won't allow it as it's not a good habit), you could use the following: QFrame#frame_0 { border: 0; }
来源:https://stackoverflow.com/questions/65163546/pyqt-widget-border-color-transparent-but-button-inside-normal-border