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. How can I set timer in order to change the picture in the label in Qt

How can I set timer in order to change the picture in the label in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
qt5.5timerqtimerqpixmap
5 Posts 3 Posters 2.4k Views
  • 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.
  • G Offline
    G Offline
    greencow
    wrote on last edited by
    #1

    I'm trying to change the picture in the label after the time interval as 12s. But it won't work, can anyone please help! The code is attached. Besides that, due to the picture size is large, thus it cannot use QVector< QPixmap > picArray to store the picture. Is there any way to realise what I wish ? Appreciate for the help!

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QWidget>
    #include <QMouseEvent>
    #include <QVector>
    #include <QPixmap>
    #include <QTimer>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
         explicit MainWindow(QWidget *parent = nullptr);
         ~MainWindow();
    
        void mouseMoveEvent(QMouseEvent *event);
    
        void update();
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
        QVector<QPixmap> picArray;
        QTimer *timer;
        int picCounter;
        int timerInterval;
    };
    
    #endif // MAINWINDOW_H
    

    This is the code for mainwindow.h

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ui->lineEdit->setStyleSheet("QLineEdit {color: white;}");
    
        setMouseTracking(true);
        ui->centralWidget->setMouseTracking(true);
    
        timer = new QTimer;
        picCounter = 0;
        timerInterval = 12000;
        connect(timer, SIGNAL(timeout()), this, SLOT(update()));
        timer->start(timerInterval);
    
    }
    
    void MainWindow::mouseMoveEvent(QMouseEvent *event)
    {
        setMouseTracking(true);
        ui->lineEdit->setText(QString(tr("move to:(%1, %2)")).arg(QString::number(event->x()), QString::number(event->y())));
    }
    
    void MainWindow::update()
    {
        timer->setInterval(timerInterval);
        QPixmap p0(":/movies/ralph.png");
        QPixmap p1(":/movies/polis.png");
        QPixmap p2(":/movies/robin.png");
        if (picCounter == 0)
        {
            ui->label_2->setPixmap(p0);
            ui->label_2->setScaledContents(true);
            ui->label_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        }
        else if (picCounter == 1)
        {
            ui->label_2->setPixmap(p1);
            ui->label_2->setScaledContents(true);
            ui->label_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        }
        else if (picCounter == 2)
        {
            ui->label_2->setPixmap(p2);
            ui->label_2->setScaledContents(true);
            ui->label_2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        }
    // update picture
        picCounter++;
        if (picCounter == 3)
            picCounter = 0;
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    This is the code for mainwindow.cpp and the picture is stored under the resource file (qrc)

    Hope to get some help! Thanks a lot!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @greencow said in How can I set timer in order to change the picture in the label in Qt:

      void update();

      your slot is not declared as slot therefore the connect can't work. You will also get a warning during the runtime of your program about this. Also I won't call the function update() since this is also a function of QWidget iirc.

      You should use the new signal/slot syntax nowadays to avoid such errors like above:

      connect(timer, &QTimer::timeout, this, &MainWindow::update);

      thus it cannot use QVector< QPixmap > picArray

      Don't understand this... esp. since you're loading all pictures in your update function every time no matter what picture is really used later on which is for sure more expensive then string them all in a vector.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      G 1 Reply Last reply
      6
      • Christian EhrlicherC Christian Ehrlicher

        @greencow said in How can I set timer in order to change the picture in the label in Qt:

        void update();

        your slot is not declared as slot therefore the connect can't work. You will also get a warning during the runtime of your program about this. Also I won't call the function update() since this is also a function of QWidget iirc.

        You should use the new signal/slot syntax nowadays to avoid such errors like above:

        connect(timer, &QTimer::timeout, this, &MainWindow::update);

        thus it cannot use QVector< QPixmap > picArray

        Don't understand this... esp. since you're loading all pictures in your update function every time no matter what picture is really used later on which is for sure more expensive then string them all in a vector.

        G Offline
        G Offline
        greencow
        wrote on last edited by greencow
        #3

        @Christian-Ehrlicher Thank you for replying ! I'm new to this ide, a lot of things unfamiliar. Thanks for pointing out the mistake! I really appreciate it! The program can now work as what I wish to! Thank you! One more thing I would like to ask how I can make the size of the push button increase when the mouse/cursor on it ? What I did is using the coordinate/position such that if the mouse/cursor in between the region of the push button then the size of push button change but it won't work.

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          @greencow

          Please mark this issue to solved. Also for your other question please raise one more topic.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          G 1 Reply Last reply
          0
          • dheerendraD dheerendra

            @greencow

            Please mark this issue to solved. Also for your other question please raise one more topic.

            G Offline
            G Offline
            greencow
            wrote on last edited by
            #5

            @dheerendra Okay, done, thank you for reminding!

            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