Stacked Widget - changing background image on StyleSheet
-
Hello all,
I'm using Qt Creator 4.11.0 Based on Qt 5.12.8 (GCC 9.3.0, 64 bit).
I have a stacked widget & am using stylesheets to display background images on it.
When a timer reaches a certain value I'd like to change the background image (& retain the active widgets on the page) on page 2 (named page2) of the stacked widget. I've spent a bit of time trying various solutions but not having much luck. I've tried various solutions/ code so far including:
ui->stackedWidget->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)"); ui->stackedWidget("page2")->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)"); QStackedWidget->("page2")->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)");
Can anyone suggest the correct syntax?
Thanks,
-
@leo738
As per the docs:ui->stackedWidget->widget(indexOfPage2)->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)");
If you mean you do not know which page in the stacked widget corresponds to your "on page 2 (named page2)", you must explain what you mean/did for "named page2".
-
@JonB said in Stacked Widget - changing background image on StyleSheet:
ui->stackedWidget->widget(indexOfPage2)->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)");
Thanks for the suggestion but I'm getting:
error: ‘indexOfPage2’ was not declared in this scope
If you mean you do not know which page in the stacked widget corresponds to your "on page 2 (named page2)", you must explain what you mean/did for "named page2".
Apologies for not being clearer. My stacked widget (created in Qt Creator) has objectNames 'page1' & 'page2'. Its the background on 'page2' that I'm trying to change after a timer expires.
-
@leo738 Then use https://doc.qt.io/qt-5/qstackedwidget.html#indexOf to get the index of your page1 and page2. And then do what @JonB suggested using the index.
-
@leo738 said in Stacked Widget - changing background image on StyleSheet:
error: ‘indexOfPage2’ was not declared in this scope
Well of course! You are supposed to understand code, not just copy it! That is where you replace by whatever the index is of your desired page ---
1
?2
? I don't know. I really didn't expect to have to say that....UPDATE
I see you have posted with brain re-engaged and your second widget is indeed at index1
, so that's simplest :)