Search code examples
pythoncolorspyqt

Modify border color of a Qgroupbox without modify borders of widgets inside it in PyQt5


I'm trying to modify the color border of a groupbox but when I do so it modifies also the border of inside widget like :

enter image description here

but I'm trying to get something like : enter image description here

Here is the code I have so far:

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

The important line is self.GB.setStyleSheet("QGroupBox { border: 1px solid red;}"). It changes the border color but it also propagates the color toward subGroupboxes and I don't want that.

Do somebody has a solution?


Solution

  • You need to name your object (GroupBox) and apply the stylesheet directly to the name. Add this to your code:

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

    Here is your modified code:

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

    The result is this:

    Name widget and add stylesheet to it