PyQt小部件边框颜色透明-但是按钮在普通边框内 [英] PyQt widget border color transparent - but button inside normal border

查看:86
本文介绍了PyQt小部件边框颜色透明-但是按钮在普通边框内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的QTWidgets.QWidget的边框是透明的或禁用的(borderwidth = 0),但是当我通过样式表将其设置为"border:0&"时,小部件内的所有按钮也都没有border-任何想法如何保持按钮的样式并仅更改小部件边框?

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

推荐答案

样式表根据定义为层叠.从维基百科描述:

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

这意味着如果泛型"在没有指定"target"的情况下在对象上设置属性.无论如何,每个子对象都将继承该属性.由于按钮是框架的子级,因此边框将被其继承.

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.

这意味着使用简单的"border:0;"来表示.这将导致不仅在框架上使用该边框,而且还会在其所有子元素上使用该边框,这就是为什么使用这种通用样式表"即使它们起作用也应避免.

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选择器在语法文档中列出,在您的情况下,您有两种选择:

Qt selectors are listed in the syntax documentation, and in your case you have two options:

  • 设置对象 class 的样式表: QFrame {border:0;}
  • 使用对象名称:#frame_0 {border:0;}
  • set the stylesheet for the object class: QFrame { border: 0; }
  • use the object name: #frame_0 { border: 0; }

显然,可以根据需要混合使用选择器.更具体地说,由于理论上更多的对象可以具有相同的对象名称(但Designer不允许这样做,因为这不是一个好习惯),因此可以使用以下代码: QFrame#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; }

这篇关于PyQt小部件边框颜色透明-但是按钮在普通边框内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆