QTimer: Getting Started problems



  • 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
    @



  • 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.


  • Lifetime Qt Champion

    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


  • Moderators

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

    Edit: nvm ... too late :)



  • 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()));@


  • Moderators

    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));

    }
    @


  • Moderators

    Wow, we really flooded the poor guy :)


  • Moderators

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

    yep...within 3 mins ... lol


  • Lifetime Qt Champion

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

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


  • Moderators

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



  • Hey, don't knock it!

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

    Thanks for all your help!

    James



  • 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()
    {

    }
    @


  • Lifetime Qt Champion

    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


  • Moderators

    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.



  • 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()
    {

    }
    @


  • Moderators

    In your connect statement:

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

    Instead:
    @
    SLOT(close())
    @


  • Lifetime Qt Champion

    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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.