c++qtc++11qt5qt5.5

Change QGroupBox's content background


I want to change the background of a QGroupBox, however I would like to only change the inside background (the darker shade of gray below each QGroupBox's title) as shown here: enter image description here What I currently have is

QGroupBox {
    background-color: red;
    border: 3px dashed black;
}

which changes the background of the entire QGroupBox like this: enter image description here

Is there a way in Qt to only change the "interior box" background rather than the whole container? Thank you in advance.


Solution

  • I guess there are 2 QGroupBox'es involved here, since that is not really clear from your post. Or is there a group box and some other inner container widget?

    In either case you should be able to use stylesheets like following:

    QGroupBox {
        background-color: red;
        margin-top:1em;
    }
    QGroupBox QGroupBox {
        background-color: green;
    }
    QGroupBox::title {
        subcontrol-origin: padding;
        subcontrol-position: left top;
        background: transparent;
        margin-top: -2.5em;
    }
    

    This will give you following result:

    Example

    You can of course replace the inner group box by an arbitrary widget.