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. QTimer: Getting Started problems

QTimer: Getting Started problems

Scheduled Pinned Locked Moved General and Desktop
17 Posts 6 Posters 6.0k 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.
  • J Offline
    J Offline
    jastmc
    wrote on last edited by
    #1

    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
    0
    • B Offline
      B Offline
      bodzio131
      wrote on last edited by
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          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
          0
          • T Offline
            T Offline
            tomma
            wrote on last edited by
            #5

            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
            0
            • sierdzioS Offline
              sierdzioS Offline
              sierdzio
              Moderators
              wrote on last edited by
              #6

              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
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                Wow, we really flooded the poor guy :)

                (Z(:^

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  [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
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    [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
                    0
                    • sierdzioS Offline
                      sierdzioS Offline
                      sierdzio
                      Moderators
                      wrote on last edited by
                      #10

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

                      (Z(:^

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jastmc
                        wrote on last edited by
                        #11

                        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
                        0
                        • J Offline
                          J Offline
                          jastmc
                          wrote on last edited by
                          #12

                          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
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            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
                            0
                            • raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by
                              #14

                              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
                              0
                              • J Offline
                                J Offline
                                jastmc
                                wrote on last edited by
                                #15

                                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
                                0
                                • sierdzioS Offline
                                  sierdzioS Offline
                                  sierdzio
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  In your connect statement:

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

                                  Instead:
                                  @
                                  SLOT(close())
                                  @

                                  (Z(:^

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

                                    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
                                    0

                                    • Login

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