c++qtstylesheet

How can I modify an existing Qt stylesheet?


For example:
if I want to create buttons, which, when pressed, each modify a single aspect of the stylesheet:

The trick here, though, is that I don't want to store all the variables and rebuild the style sheet on each button press. I would like, for example, to have a simple:

this->setStyleSheet(this->getStylesheet()+"margin-left: 10px:")

Solution

  • Here is the code in main.cpp:

    QWidget wdg;
    QHBoxLayout hlay;
    wdg.setStyleSheet("border:2px solid rgb(74, 74, 74);");
    
    QPushButton btn;
    btn.setStyleSheet("border-radius:5px;");
    btn.setText("Hello");
    
    QPushButton btn2;
    btn2.setStyleSheet("background-color: rgb(190, 190, 190);");
    btn2.setText("Hello");
    
    hlay.addWidget(&btn);
    hlay.addWidget(&btn2);
    
    qDebug()<<btn.styleSheet();
    
    wdg.setLayout(&hlay);
    wdg.show();
    

    Setting and getting the style sheet works with QString, and so, you can use + operator.