Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QTimer: Getting Started problems

    General and Desktop
    6
    17
    5243
    Loading More Posts
    • 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.
    • J
      jastmc last edited by

      I am a beginner, trying to get a QTimer to drive a simple counter. It should count up in seconds but the slot never gets called. What am I doing wrong? I want the timer to increment the state every second so its displayed on the screen.

      This is my program:-

      @#include "mita_screen.h"
      #include "ui_mita_screen.h"

      mita_screen::mita_screen(QWidget *parent) :
      QDialog(parent),
      ui(new Ui::mita_screen)
      {
      mita_screen::setWindowFlags(Qt::SplashScreen);
      ui->setupUi(this);

      mita_state = 0;     // Initialise the state machine
      
      QTimer *timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(state_update));
      timer->start(1000);
      

      }

      mita_screen::~mita_screen()
      {
      delete ui;
      }

      void mita_screen::state_update()
      {
      mita_state++;

      }

      void mita_screen::paintEvent(QPaintEvent *)
      {
      QString data;

      data = QString("%1")
              .arg(mita_state);
      
      ui->label->setText(data);
      

      }

      void mita_screen::on_pushButton_clicked()
      {
      delete ui;
      }
      @

      This is my main:-

      @#include "mita_screen.h"
      #include <QApplication>

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      mita_screen w;
      w.show();

      return a.exec&#40;&#41;;
      

      }
      @

      This is my header:-

      @#ifndef MITA_SCREEN_H
      #define MITA_SCREEN_H

      #include <QDialog>
      #include <QtGui>

      namespace Ui {
      class mita_screen;
      }

      class mita_screen : public QDialog
      {
      Q_OBJECT

      public:
      explicit mita_screen(QWidget *parent = 0);
      ~mita_screen();
      int mita_state;

      protected:
      void paintEvent(QPaintEvent *event);

      private slots:
      void on_pushButton_clicked();
      void state_update();

      private:
      Ui::mita_screen *ui;
      };
      #endif // MITA_SCREEN_H
      @

      1 Reply Last reply Reply Quote 0
      • B
        bodzio131 last edited by

        I'm not sure you can omit brackets for slot state_update, probably should be:
        @
        connect( timer, SIGNAL( timeout() ), this, SLOT( state_update() ) );
        @

        Instead of QTimer you could also use QDialog::startTimer and virtual QDialog::timerEvent.

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Remove the paintEvent, you are not painting anything.

          @
          void mita_screen::state_update()
          {
          ui->label->setText(QString("%1").arg(++mita_state));
          }@

          Is enough to update the label at on your timer's time out.

          Why do you delete your ui variable in on_pushButton_clicked ? Also note, that it's already deleted in the destructor, so calling that function, then the destructor will cause a crash.

          If you want to close the dialog, connect the pushButton clicked signal on the close slot of the dialog.

          Hope it helps

          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 Reply Quote 0
          • raven-worx
            raven-worx Moderators last edited by

            change your connect-statement to:
            @connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));@

            Edit: nvm ... too late :)

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • T
              tomma last edited by

              You just increment mita_state but never update it to screen.
              Remove your "paintEvent":http://qt-project.org/doc/qt-4.8/qwidget.html#paintEvent implementation, which is all wrong, and update label text in state_update().

              Also your connect call in line 14 should be @connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));@

              1 Reply Last reply Reply Quote 0
              • sierdzio
                sierdzio Moderators last edited by

                Slot connection is probably wrong

                No need to reimplement the paint event

                This should work:
                @
                mita_screen::mita_screen(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::mita_screen)
                {
                mita_screen::setWindowFlags(Qt::SplashScreen);
                ui->setupUi(this);

                mita_state = 0;     // Initialise the state machine
                
                QTimer *timer = new QTimer(this);
                connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));
                timer->start(1000);
                

                }

                void mita_screen::state_update()
                {
                mita_state++;
                ui->label->setText(QString::number(mita_state));

                }
                @

                (Z(:^

                1 Reply Last reply Reply Quote 0
                • sierdzio
                  sierdzio Moderators last edited by

                  Wow, we really flooded the poor guy :)

                  (Z(:^

                  1 Reply Last reply Reply Quote 0
                  • raven-worx
                    raven-worx Moderators last edited by

                    [quote author="sierdzio" date="1366112776"]Wow, we really flooded the poor guy :)[/quote]

                    yep...within 3 mins ... lol

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply Reply Quote 0
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      [quote author="sierdzio" date="1366112776"]Wow, we really flooded the poor guy :)[/quote]

                      Like one other user said, we're parallelizing :D

                      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 Reply Quote 0
                      • sierdzio
                        sierdzio Moderators last edited by

                        Yeah, but it would be a tad better to spread across different threads (pun well intended :)).

                        (Z(:^

                        1 Reply Last reply Reply Quote 0
                        • J
                          jastmc last edited by

                          Hey, don't knock it!

                          Its much better to get too many responses that none at all.

                          Thanks for all your help!

                          James

                          1 Reply Last reply Reply Quote 0
                          • J
                            jastmc last edited by

                            SGaist said:-

                            bq. Why do you delete your ui variable in on_pushButton_clicked ? Also note, that it’s already deleted in the destructor, so calling that function, then the destructor will cause a crash.
                            If you want to close the dialog, connect the pushButton clicked signal on the close slot of the dialog.

                            I changed my code to connect the on_pushButton_clicked() signal to the ~mita_screen() slot but the button has no effect when pressed. See below.

                            Can anyone please tell me whats wrong here?

                            @#include "mita_screen.h"
                            #include "ui_mita_screen.h"

                            mita_screen::mita_screen(QWidget *parent) :
                            QDialog(parent),
                            ui(new Ui::mita_screen)
                            {
                            mita_screen::setWindowFlags(Qt::SplashScreen);
                            ui->setupUi(this);

                            mita_state = 0;     // Initialise the state machine
                            
                            QTimer *timer = new QTimer(this);
                            connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));
                            timer->start(1000);
                            
                            connect(ui->pushButton, SIGNAL(on_pushButton_clicked()), this, SLOT(~mita_screen()));
                            

                            }

                            mita_screen::~mita_screen()
                            {
                            delete ui;
                            }

                            void mita_screen::state_update()
                            {
                            ui->label->setText(QString("%1").arg(++mita_state));

                            }

                            void mita_screen::on_pushButton_clicked()
                            {

                            }

                            void mita_screen::on_pushButton_pressed()
                            {

                            }
                            @

                            1 Reply Last reply Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              You're connecting to the destructor:

                              1.It's not a slot
                              2.Never call a destructor, it's done for you when the object is destroyed.

                              As I said, connect to the "close slot":http://qt-project.org/doc/qt-4.8/qwidget.html#close

                              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 Reply Quote 0
                              • raven-worx
                                raven-worx Moderators last edited by

                                you can't connect to a destructor. Since your widget is a dialog connect it to the close() slot and additionaly to the deleteLater() slot or set the "widget attribute":http://qt-project.org/doc/qt-4.8/qwidget.html#setAttribute: to Qt::WA_DeleteOnClose to let Qt delete the dialog for you.

                                --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                If you have a question please use the forum so others can benefit from the solution in the future

                                1 Reply Last reply Reply Quote 0
                                • J
                                  jastmc last edited by

                                  Sorry, that was an older version, somehow. Thanks for your patience!
                                  With this code, the button is ignored and has no effect.

                                  What I meant to post was:-

                                  @#include "mita_screen.h"
                                  #include "ui_mita_screen.h"
                                  #include <QtGui>
                                  #include <QLabel>

                                  mita_screen::mita_screen(QWidget *parent) :
                                  QDialog(parent),
                                  ui(new Ui::mita_screen)
                                  {
                                  mita_screen::setWindowFlags(Qt::SplashScreen);
                                  ui->setupUi(this);

                                  mita_state = 0;     // Initialise the state machine
                                  
                                  QTimer *timer = new QTimer(this);
                                  connect(timer, SIGNAL(timeout()), this, SLOT(state_update()));
                                  timer->start(1000);
                                  
                                  connect(ui->pushButton, SIGNAL(on_pushButton_clicked()), this, SLOT(mita_screen::close() ));
                                  connect(ui->pushButton, SIGNAL(on_pushButton_clicked()), this, SLOT(mita_screen::deleteLater() ));
                                  

                                  }

                                  mita_screen::~mita_screen()
                                  {
                                  delete ui;
                                  }

                                  void mita_screen::state_update()
                                  {
                                  static int counter=0;

                                  ui->label->setText(QString("%1").arg(++mita_state));
                                  

                                  }

                                  void mita_screen::on_pushButton_clicked()
                                  {

                                  }

                                  void mita_screen::on_pushButton_pressed()
                                  {

                                  }
                                  @

                                  1 Reply Last reply Reply Quote 0
                                  • sierdzio
                                    sierdzio Moderators last edited by

                                    In your connect statement:

                                    Not:
                                    @
                                    SLOT(mita_screen::close())
                                    @

                                    Instead:
                                    @
                                    SLOT(close())
                                    @

                                    (Z(:^

                                    1 Reply Last reply Reply Quote 0
                                    • SGaist
                                      SGaist Lifetime Qt Champion last edited by

                                      on_pushButton_clicked is a slot.

                                      Try this:
                                      @
                                      connect(ui->pushButton, SIGNAL(clicked()), SLOT(close()));
                                      @

                                      Have a look at the "signal/slot":http://qt-project.org/doc/qt-4.8/signalsandslots.html and the "designer":http://qt-project.org/doc/qt-4.8/designer-manual.html documentation to have a better understanding of how it works

                                      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 Reply Quote 0
                                      • First post
                                        Last post