在 PyQt5 中修改 Qgroupbox 的边框颜色而不修改其内部小部件的边框

问题描述 投票:0回答:3

我正在尝试修改组框的颜色边框,但是当我这样做时,它也会修改内部小部件的边框,例如:

但我想得到类似的东西:

这是我到目前为止的代码:

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import * 

class tabdemo(QMainWindow):
    def __init__(self):
        super(tabdemo, self).__init__()
        self.setGeometry(50,50,500,500)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainB  = QVBoxLayout()

        self.GB = QGroupBox("GroupBox")
        self.GB.setStyleSheet("QGroupBox { border: 1px solid red;}")
        self.GB.setFixedWidth(100)
        self.mainHBOX  = QVBoxLayout()

        self.GB1 = QGroupBox("GroupBox1")
        self.GB1.setFixedHeight(100)
        self.GB2 = QGroupBox("GroupBox2")
        self.GB2.setFixedHeight(100)
        self.GB3 = QGroupBox("GroupBox3")
        self.GB3.setFixedHeight(100)
        self.mainHBOX.addWidget(self.GB1)
        self.mainHBOX.addWidget(self.GB2)
        self.mainHBOX.addWidget(self.GB3)

        self.GB.setLayout(self.mainHBOX)

        self.mainB.addWidget(self.GB)

        self.centralWidget.setLayout(self.mainB)





def main():
   app = QApplication(sys.argv)
   ex = tabdemo()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

重要的一行是

self.GB.setStyleSheet("QGroupBox { border: 1px solid red;}")
。它改变了边框颜色,但它也将颜色传播到子组框,我不希望这样。

有人有解决办法吗?

python colors pyqt
3个回答
8
投票

您需要命名您的对象(GroupBox)并将样式表直接应用于该名称。将其添加到您的代码中:

        self.GB.setObjectName("ColoredGroupBox")  # Changed here...
        self.GB.setStyleSheet("QGroupBox#ColoredGroupBox { border: 1px solid red;}")  # ... and here

这是您修改后的代码:

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class tabdemo(QMainWindow):
    def __init__(self):
        super(tabdemo, self).__init__()
        self.setGeometry(50,50,500,500)
        self.centralWidget = QWidget()
        self.setCentralWidget(self.centralWidget)
        self.mainB  = QVBoxLayout()

        self.GB = QGroupBox("GroupBox")
        self.GB.setObjectName("ColoredGroupBox")  # Changed here...
        self.GB.setStyleSheet("QGroupBox#ColoredGroupBox { border: 1px solid red;}")  # ... and here
        self.GB.setFixedWidth(100)
        self.mainHBOX  = QVBoxLayout()

        self.GB1 = QGroupBox("GroupBox1")
        self.GB1.setFixedHeight(100)
        self.GB2 = QGroupBox("GroupBox2")
        self.GB2.setFixedHeight(100)
        self.GB3 = QGroupBox("GroupBox3")
        self.GB3.setFixedHeight(100)
        self.mainHBOX.addWidget(self.GB1)
        self.mainHBOX.addWidget(self.GB2)
        self.mainHBOX.addWidget(self.GB3)

        self.GB.setLayout(self.mainHBOX)

        self.mainB.addWidget(self.GB)

        self.centralWidget.setLayout(self.mainB)

def main():
   app = QApplication(sys.argv)
   ex = tabdemo()
   ex.show()
   sys.exit(app.exec_())

if __name__ == '__main__':
   main()

结果是这样的:


2
投票

我注意到,对外部 GroupBox 的更改会将标签向下移动。我想你也需要

setStyleSheet("QGroupBox::title {padding 0 3 px }")


0
投票

接受的答案将使groupbox的标题沉没。

试试这个

QGroupBox {
    border: 1px solid rgb(182, 182, 182);
    margin: 10px
}

QGroupBox::title {
    left: 10px;
    top: -8px;
}
© www.soinside.com 2019 - 2024. All rights reserved.