Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Stacked Widget - changing background image on StyleSheet
Forum Updated to NodeBB v4.3 + New Features

Stacked Widget - changing background image on StyleSheet

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    leo738
    wrote on 23 Sept 2021, 11:59 last edited by
    #1

    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,

    J 1 Reply Last reply 23 Sept 2021, 12:09
    0
    • L leo738
      23 Sept 2021, 11:59

      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,

      J Offline
      J Offline
      JonB
      wrote on 23 Sept 2021, 12:09 last edited by JonB
      #2

      @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".

      1 Reply Last reply
      0
      • L Offline
        L Offline
        leo738
        wrote on 23 Sept 2021, 12:29 last edited by
        #3

        @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.

        J 1 Reply Last reply 23 Sept 2021, 12:41
        0
        • L Offline
          L Offline
          leo738
          wrote on 23 Sept 2021, 12:39 last edited by
          #4

          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!

          J 1 Reply Last reply 23 Sept 2021, 12:40
          0
          • L leo738
            23 Sept 2021, 12:39

            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!

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 23 Sept 2021, 12:40 last edited by
            #5

            @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.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • L leo738
              23 Sept 2021, 12:29

              @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.

              J Offline
              J Offline
              JonB
              wrote on 23 Sept 2021, 12:41 last edited by
              #6

              @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 index 1, so that's simplest :)

              1 Reply Last reply
              1

              1/6

              23 Sept 2021, 11:59

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved