-1
// MyDialog.h //

#pragma once
#include <QProgressDialog>
#include <QString>
#include <QWidget>
class MyDialog : public QProgressDialog {

    Q_OBJECT

 public:
    explicit MyDialog(const QString &text, QWidget *parent = nullptr) : QProgressDialog(text, nullptr, 0, 0, parent) {
        qDebug() << "MyDialog()!";
    }
    virtual ~MyDialog() override {
        qDebug() << "~MyDialog()!";
    }
};
// main.cpp //

#include "MyDialog.h"
#include <QApplication>

const char *qss =
    "MyDialog {"
        "min-width: 260px; background-color: peachpuff;" // qss line 1
    "}"
    "MyDialog QLabel {"
        "font-size: 16px; color: red;" // qss line 2
    "}"
    "MyDialog QProgressBar {"
        "text-align: center; border-radius: 12px; border: 1px solid grey;" // qss line 3
    "}";

int main(int argc, char **argv) {
    QApplication qApp(argc, argv);
    qApp.setStyleSheet(qss);   // 1、set qss in QApplication instance(only). (like image1 below)

    MyDialog dlg("Loading, Please wait...");
    //dlg.setStyleSheet(qss);  // 2、set qss in MyDialog instance(only). (like image2 below)
    dlg.show();

    return qApp.exec();
}

image1, where QSS is applied on QApplication instance:

enter image description here

iamge2, where QSS is applied on MyDialog instance:

enter image description here

When I set QSS to QApplication instance,only "qss line 1" works.

When I set QSS to MyDialog instance,all 3 QSS lines work.

Why? Is there a bug with my code or with Qt framework?

How to make all three lines of QSS code work when I use: qApp.setStyleSheet(qss)?

Environment:

  • Windows 10(1809)
  • Qt 6.5.2(MSVC 2019 64-bit)
  • CMake 3.27.1
  • Ninja 1.11.1
Paul
  • 1
  • 2
  • Try calling `qApp.setStyleSheet(qss);` after you instantiate `MyDialog` object. – Abderrahmene Rayene Mihoub Aug 11 '23 at 12:44
  • What happens if you swap qss1 and qss3 ? Whether qss1 works regardless of where it is placed in the style sheet or only the first item that appears in the style sheet works, this will lead to different investigations. – Atmo Aug 13 '23 at 13:30

0 Answers0