timer problem(timer does not work)
hi
form1.h
Code:
#ifndef FORM1_H
#define FORM1_H
#include <QString>
#include <QWidget>
#include <QTimer>
#include <QTime>
namespace Ui {
class Form1;
}
{
Q_OBJECT
public:
explicit Form1
(QWidget *parent
= 0);
~Form1();
int i;
private:
Ui::Form1 *ui;
private:
public slots:
void startTime();
void showTime();
void stopTime();
};
#endif // FORM1_H
form1.cpp
Code:
#include "form1.h"
#include "ui_form1.h"
#include <QTimer>
#include <QTime>
ui(new Ui::Form1)
{
ui->setupUi(this);
ui->lcdNumber->setNumDigits(8);
time.setHMS(0,0,0,0);
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()
{
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
Re: timer problem(timer does not work)
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.
Re: timer problem(timer does not work)
i can't understand where is the problem.please help me.
Re: timer problem(timer does not work)
Code:
{
}
ui(new Ui::Form1)
{
}
They are not the same! You have to initialize your member correctly. Read any book about C++.
Re: timer problem(timer does not work)
Now timer is working but cannot stop.
Code:
#include "form1.h"
#include "ui_form1.h"
#include <QTimer>
#include <QTime>
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()
{
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start(1000);
}
void Form1::showTime()
{
i=i+1;
newtime=time.addSecs(i);
QString text
= newtime.
toString("hh:mm:ss");
ui->lcdNumber->display(text);
}
void Form1::stopTime()
{
timer->stop();
}
Re: timer problem(timer does not work)
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++.
Re: timer problem(timer does not work)
now working...
form1.cpp
Code:
#include "form1.h"
#include "ui_form1.h"
#include <QTimer>
#include <QTime>
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()
{
connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
timer->start(1000);
}
void Form1::showTime()
{
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
Code:
#ifndef FORM1_H
#define FORM1_H
#include <QString>
#include <QWidget>
#include <QTimer>
#include <QTime>
namespace Ui {
class Form1;
}
{
Q_OBJECT
public:
explicit Form1
(QWidget *parent
= 0);
~Form1();
int i;
private:
Ui::Form1 *ui;
private:
public:
public slots:
void startTime();
void showTime();
void stopTime();
};
#endif // FORM1_H