#include <QtCore>
#include <QtGui>
{
Q_OBJECT
public:
M1() {
count = 0;
setWindowTitle("Timer on stack");
setCentralWidget(l);
connect(&t, SIGNAL(timeout()), this, SLOT(onTimeout()));
t.start(500);
};
public slots:
void onTimeout
() { l
->setText
(QString::number(++count,
10));
};
private:
int count;
};
{
Q_OBJECT
public:
M2() {
count = 0;
setWindowTitle("Timer on heap");
setCentralWidget(l)
t
= new QTimer(this);
// Qt will look after delete-ing the timer connect(t, SIGNAL(timeout()), this, SLOT(onTimeout()));
t->start(500);
};
public slots:
void onTimeout
() { l
->setText
(QString::number(++count,
10));
};
private:
int count;
};
int main(int argc, char **argv)
{
M1 m1;
M2 m2;
m1.show();
m2.show();
return a.exec();
}
#include <QtCore>
#include <QtGui>
class M1: public QMainWindow
{
Q_OBJECT
public:
M1() {
count = 0;
setWindowTitle("Timer on stack");
l = new QLabel("", this);
setCentralWidget(l);
connect(&t, SIGNAL(timeout()), this, SLOT(onTimeout()));
t.start(500);
};
public slots:
void onTimeout() { l->setText(QString::number(++count, 10)); };
private:
QTimer t;
QLabel *l;
int count;
};
class M2: public QMainWindow
{
Q_OBJECT
public:
M2() {
count = 0;
setWindowTitle("Timer on heap");
l = new QLabel("", this);
setCentralWidget(l)
t = new QTimer(this); // Qt will look after delete-ing the timer
connect(t, SIGNAL(timeout()), this, SLOT(onTimeout()));
t->start(500);
};
public slots:
void onTimeout() { l->setText(QString::number(++count, 10)); };
private:
QTimer *t;
QLabel *l;
int count;
};
int main(int argc, char **argv)
{
QApplication a(argc, argv);
M1 m1;
M2 m2;
m1.show();
m2.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks