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

时间:2017-11-21 14:14:00

标签: python colors pyqt

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

enter image description here

但是我想要得到类似的东西: enter image description 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.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;}")。它会改变边框颜色,但它也会将颜色传播到子组框,我不希望这样。

有人有解决方案吗?

2 个答案:

答案 0 :(得分:2)

您需要命名对象(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()

结果如下:

Name widget and add stylesheet to it

答案 1 :(得分:1)

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

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