Home > Back-end >  How to maintain default background color of each items inside Qt group box?
How to maintain default background color of each items inside Qt group box?

Time:11-02

When I changed the background color of Qt group box so combobox background color is also changed. Which is inside the group box. I want default color of combobox so this is why i am not changing the bg-color of combobox. Please tell me how can I change the background color of Qt group box without changing default bg-color of inside items. I changed the background of QT group box using style sheet in qt designer (ui). I am beginner please help.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

CodePudding user response:

you should follow these steps :

  1. set specific names for your objects :

enter image description here

  1. select parent object and add a stylesheet to the parent like this :

enter image description here

  1. this is the Stylesheet :

    QGroupBox#gBox1 { background-color: rgb(138, 226, 52); }

first, you should set which kind of class you want like QGroupBox, and for set style, to the specific object you call its object name after #.

  1. out put :

enter image description here

CodePudding user response:

enter image description here

Simple project with styles

#include <QtCore>
#include <QtGui>
#include <QtWidgets>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGroupBox GroupBox;
    GroupBox.setMinimumSize(QSize(400, 400));
    GroupBox.setStyleSheet("QGroupBox {background-color: green}");
    
    QComboBox Combo1, Combo2;
    Combo1.setStyleSheet("QComboBox {background-color: yellow}");
    Combo2.setStyleSheet("QComboBox {background-color: red}");

    Combo1.addItem("Test1");
    Combo1.addItem("Test2");

    Combo2.addItem("Test3");
    Combo2.addItem("Test4");

    QVBoxLayout vbox;
    vbox.addWidget(&Combo1);
    vbox.addWidget(&Combo2);

    GroupBox.setLayout(&vbox);
    GroupBox.show();

    return a.exec();
}

also you can change object name 'setObjectName(const QString &)' function and then style different objects using there names

    Combo1.setObjectName("TestObject");
    Combo1.setStyleSheet("QComboBox#TestObject {background-color: yellow}");
  • Related