Important: Please read the Qt Code of Conduct -

Delay time in stylesheet loading

  • Hi everyone,

    I made a simple application that should set a stylesheet loading settings from a file.
    I put this->setStylesheet() in the MainWindow 's constructor, right after setupUI().
    It happens that at first mainwindow shows itself plain, and after less than a second, modifications are applied.
    This is an annoying graphic problem.

  • Moderators

    Set the stylesheet in your main.cpp, using QApplication::setStylesheet().

  • if I call a.setStylesheet("background-color:red;") for example, a Qt wanring "Could not parse application stylesheet" is displayed".
    If you mean i have to call some static method QApplication::setStylesheet() the compiler stops.

  • Moderators

    No, I do not mean static methods. Try a more explicit QSS, like:
    QMainWindow {

  • Yes it works, but no evident timing improvement.
    It seems like if stylesheet modifications are processed after showevent...

    In such a case I'd prefer waiting for stylesheet rendering and show the main window after this have happen.
    But I don't know how.

  • Moderators

    Try this:
    // in main.cpp
    QTimer::singleShot(800, mainWindowPointer, SLOT(show()));
    return a.exec();

    It should delay the showing of your mainWindow for 800 ms.

  • That's funny, it waits 800ms, shows the plain white window, and change the stylesheet with evident time lapse.
    Even if I change the stylesheet form the UI form the effect is the same, and I forgot to say, but it's obvious, that timing differences depends on the stylesheet type, it increase for gradients for example.

    Mybe the sequence is that the stylesheet engine works after the show event...

  • I noticed that it has the same behaviour if I iconize the window and then I show it clicking on the icon.
    It shows the plain window and then fill the stylesheet with time.
    Maybe could be my PC extremly slow and this effetc emphasizes more than usual.

  • Moderators

    [quote author="ale82" date="1390907896"]Maybe could be my PC extremly slow and this effetc emphasizes more than usual.[/quote]

    This could be the case. I have seen some really heavy QSS code that took up to 4 seconds to load... but it never exhibited the behaviour you have mentioned. In my case, the whole GUI was loading in the background and once it was shown, it already had the styling applied.

Log in to reply