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. Wait/sleep 100ms during QPixmap in QLabel slideshow
Qt 6.11 is out! See what's new in the release blog

Wait/sleep 100ms during QPixmap in QLabel slideshow

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 3.3k 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.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by cpper
    #1

    Hi again :)

    I want to create an image slideshow, by waiting 100ms between the displayed pics. The pictures are displayed as QPixmap's in a QLabel. The slideshow should start when I'm clicking on the pushButton.

    std::vector<QPixmap> pics;
    //.......
    
    void MainWindow::on_pushButton_clicked()
    {
            for(const QPixmap& x: pics){
                ui->label->setPixmap(x);
                //wait 100ms between each pic
            }
    }
    

    I've tried using QThread::msleep(100); but this freezes the program 100*nr. of pics ms and displays the last image only.

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

      Hi
      You could use a timer and a slot and do it sort of reverse

      in constructor
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(update()));
      timer->start(1000);
      ..

      // this is called every second. adjust if need to be faster/slower
      void MainWindow::update()
      {
      if (currentimage <pics.size() )
      ui->label->setPixmap( pics[ currentimage++ ] );

      }

      1 Reply Last reply
      3
      • cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

        Thanks, I'll try this method. However, is there no simpler way to do it ? Such as a single function call. Why doesn't msleep work in this case ?

        mrjjM 1 Reply Last reply
        0
        • cpperC cpper

          Thanks, I'll try this method. However, is there no simpler way to do it ? Such as a single function call. Why doesn't msleep work in this case ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @cpper
          well you pause the GUI thread with msleep.

          The reason you dont see each image is that the for loop
          don't allow events to be processed.

          You can use QApplication::processEvents();
          in the loop and it will sort of work - but you will still make the
          GUI a big laggy as long for loops in gui thread is just not a good idea.

          cpperC 1 Reply Last reply
          1
          • mrjjM mrjj

            @cpper
            well you pause the GUI thread with msleep.

            The reason you dont see each image is that the for loop
            don't allow events to be processed.

            You can use QApplication::processEvents();
            in the loop and it will sort of work - but you will still make the
            GUI a big laggy as long for loops in gui thread is just not a good idea.

            cpperC Offline
            cpperC Offline
            cpper
            wrote on last edited by
            #5

            @mrjj
            It's clear now.

            Since I want to keep everything in the clicked() function (and not call update() slot) I used both QTimer and QEventLoop:

            
            void MainWindow::on_pushButton_clicked()
            {
                QTimer timer(this);
                QEventLoop loop;
                timer.setInterval(100);
                QObject::connect(&timer, &QTimer::timeout,&loop,&QEventLoop::quit);
            
                for(const QPixmap& x: pics){
                    ui->label->setPixmap(x);
                    timer.start();
                    loop.exec();
                }
            
            
            
            }
            

            It works as I wished now. But would it be better to use pointer to QTimer and QEventLoop in such situations ?

            mrjjM 1 Reply Last reply
            1
            • cpperC cpper

              @mrjj
              It's clear now.

              Since I want to keep everything in the clicked() function (and not call update() slot) I used both QTimer and QEventLoop:

              
              void MainWindow::on_pushButton_clicked()
              {
                  QTimer timer(this);
                  QEventLoop loop;
                  timer.setInterval(100);
                  QObject::connect(&timer, &QTimer::timeout,&loop,&QEventLoop::quit);
              
                  for(const QPixmap& x: pics){
                      ui->label->setPixmap(x);
                      timer.start();
                      loop.exec();
                  }
              
              
              
              }
              

              It works as I wished now. But would it be better to use pointer to QTimer and QEventLoop in such situations ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @cpper
              ok. super.
              Nope, in this case using non pointers is very correct since
              there is no need for pointers here. :)

              1 Reply Last reply
              1

              • Login

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