ymmx
ymmx

Reputation: 4967

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?

Upvotes: 1

Views: 10988

Answers (3)

Jcyrss
Jcyrss

Reputation: 1800

The accepted anwser will make the title of groupbox sunk.

Try this

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

QGroupBox::title {
    left: 10px;
    top: -8px;
}

Upvotes: 0

James
James

Reputation: 49

I note that this change to the outer GroupBox moves the label downward. I think you also need

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

Upvotes: 2

armatita
armatita

Reputation: 13475

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

Upvotes: 8

Related Questions