PDA

View Full Version : timer problem(timer does not work)



masuk
12th February 2011, 18:46
hi

form1.h


#ifndef FORM1_H
#define FORM1_H

#include <QString>

#include <QWidget>

#include <QTimer>
#include <QTime>


namespace Ui {
class Form1;
}

class Form1 : public QWidget
{
Q_OBJECT

public:
explicit Form1(QWidget *parent = 0);
~Form1();
int i;

private:
Ui::Form1 *ui;


private:
QTime time;
QTimer timer;

public slots:
void startTime();
void showTime();
void stopTime();

};
#endif // FORM1_H



form1.cpp


#include "form1.h"
#include "ui_form1.h"


#include <QTimer>
#include <QTime>


Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);

ui->lcdNumber->setNumDigits(8);
QTime time = QTime();
time.setHMS(0,0,0,0);
QTimer *timer=new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
i=0;
QString text = time.toString("hh:mm:ss");
ui->lcdNumber->display(text);



}

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

void Form1::startTime()
{
timer.start();

}

void Form1::showTime()
{
QTime newtime = QTime();
newtime.setHMS(0,0,0,0);
i=i+1;
newtime=time.addSecs(i);

QString text = newtime.toString("hh:mm:ss");
ui->lcdNumber->display(text);


}

void Form1::stopTime()
{
timer.stop();
}



timer is not working.timer is not starting

Lykurg
12th February 2011, 19:05
Well, you are initiate a timer in the c-tor which is not the one declared in the header file. Basic C++ failure. Read about how to initialize members correctly.

masuk
13th February 2011, 03:57
i can't understand where is the problem.please help me.

Lykurg
13th February 2011, 08:06
class Form1 : public QWidget
{
QTimer timer;
}


Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
QTimer *timer=new QTimer(this);
}They are not the same! You have to initialize your member correctly. Read any book about C++.

masuk
13th February 2011, 08:07
Now timer is working but cannot stop.


#include "form1.h"
#include "ui_form1.h"


#include <QTimer>
#include <QTime>


Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);


time.setHMS(0,0,0,0);
i=0;
ui->lcdNumber->setNumDigits(8);
QString text = time.toString("hh:mm:ss");
ui->lcdNumber->display(text);
}

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

void Form1::startTime()
{
QTimer *timer=new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start(1000);
}

void Form1::showTime()
{
QTime newtime;
i=i+1;
newtime=time.addSecs(i);
QString text = newtime.toString("hh:mm:ss");
ui->lcdNumber->display(text);

}

void Form1::stopTime()
{
QTimer *timer=new QTimer(this);
timer->stop();
}

Lykurg
13th February 2011, 08:54
Ehm, read any book about C++! You obviously have no idea what you are doing. With that poor knowledge of C++ you won't be able to do anything with Qt since it is C++ after all. So please first learn C++.

masuk
14th February 2011, 05:00
now working...
form1.cpp


#include "form1.h"
#include "ui_form1.h"

#include <QTimer>
#include <QTime>


Form1::Form1(QWidget *parent) :
QWidget(parent),
ui(new Ui::Form1)
{
ui->setupUi(this);


time.setHMS(0,0,0,0);
i=0;
ui->lcdNumber->setNumDigits(8);
QString text = time.toString("hh:mm:ss");
ui->lcdNumber->display(text);
}

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

void Form1::startTime()
{
timer=new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start(1000);
}

void Form1::showTime()
{
QTime newtime;
i=i+1;
newtime=time.addSecs(i);
QString text = newtime.toString("hh:mm:ss");
ui->lcdNumber->display(text);

}

void Form1::stopTime()
{
timer->stop();
}


form1.h


#ifndef FORM1_H
#define FORM1_H

#include <QString>

#include <QWidget>

#include <QTimer>
#include <QTime>


namespace Ui {
class Form1;
}

class Form1 : public QWidget
{
Q_OBJECT

public:
explicit Form1(QWidget *parent = 0);
~Form1();
int i;

private:
Ui::Form1 *ui;


private:
QTime time;

public:
QTimer *timer;

public slots:
void startTime();
void showTime();
void stopTime();

};
#endif // FORM1_H