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. Weird Behavior for QStackedWidget
Qt 6.11 is out! See what's new in the release blog

Weird Behavior for QStackedWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 906 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.
  • M Offline
    M Offline
    mkre
    wrote on last edited by mkre
    #1

    I want to use a Stacked Widget to switch between three different widgets depending on an event that is happening. To test this, I'm creating a StackedWidget, and then changing which widget is "active" every few seconds.

    Here is main:

    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
    
        view = new MainWindow();
        view->showImg1();
        view->show();       
        QThread::sleep(3);
    
        view->showImg2();
        QThread::sleep(3);
    
        view->showImg3();
    }
    

    And here is the .cpp for MainWindow

    MainWindow::MainWindow(QWidget *parent) : QStackedWidget(parent)
    {
        img1View = new QLabel();
        img2View = new QLabel();
        img3View = new QLabel();
    
        img1View->setPixmap("/home/1.jpg");
        img2View->setPixmap("/home/2.jpg");
        img3View->setPixmap("/home/3.jpg");
    
        this->addWidget(img1View);
        this->addWidget(img2View);
        this->addWidget(img3View);
    }
    
    MainWindow::showImg1()
    {
        this->setCurrentWidget(img1View);
        qDebug() << "Index: " << this->currentIndex();
    }
    
    MainWindow::showImg2()
    {
        this->setCurrentWidget(img2View);
        qDebug() << "Index: " << this->currentIndex();
    }
    
    MainWindow::showImg3()
    {
        this->setCurrentWidget(img3View);
        qDebug() << "Index: " << this->currentIndex();
    }
    

    So the expected behavior is to have a window open on the first image, and to cycle to the second image after 3 seconds and to the final image after 3 seconds.

    What is happening is that none of the images show except for the very last one. But the qDebug messages show up as expected and with the correct index. This leads me to think that all of the images are on the last widget, and the last image is hiding the others. How can I fix this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Don't use QThread::sleep. What happens is that you are currently blocking your application startup hence the reason you see only the last image. show will only have an effect once app.exec() has been called.

      Use QTimer::singleShot. One with 3000 and another with 6000. That will allow your application to start correctly and then change your widget as wanted.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      3
      • SGaistS SGaist

        Hi and welcome to devnet,

        Don't use QThread::sleep. What happens is that you are currently blocking your application startup hence the reason you see only the last image. show will only have an effect once app.exec() has been called.

        Use QTimer::singleShot. One with 3000 and another with 6000. That will allow your application to start correctly and then change your widget as wanted.

        M Offline
        M Offline
        mkre
        wrote on last edited by
        #3

        @SGaist Thanks!

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You're welcome !

          If you have it running properly now, please mark that thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0

          • Login

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