Stacked Widget - changing background image on StyleSheet
-
wrote on 23 Sept 2021, 11:59 last edited by
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,
-
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,
wrote on 23 Sept 2021, 12:09 last edited by JonB@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".
-
wrote on 23 Sept 2021, 12:29 last edited by
@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.
-
wrote on 23 Sept 2021, 12:39 last edited by
Apologies, brain re-engaged!
I would have posted this earlier but prevented by the system.
ui->stackedWidget->widget(1)->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)");
Works for me!
Many thanks!
-
Apologies, brain re-engaged!
I would have posted this earlier but prevented by the system.
ui->stackedWidget->widget(1)->setStyleSheet("background-image: url(:/images/newImageToDisplay.png)");
Works for me!
Many thanks!
@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.
-
@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.
wrote on 23 Sept 2021, 12:41 last edited by@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 :)
1/6