PDA

View Full Version : Problem with QTimer



hosseinyounesi
10th April 2009, 10:28
This is what I wrote to make a simple timer.
openSuse 11, Qt 4.5, Qt Creator, Qt GUI App

It doesn't work, I mean when I try to execute it "Hi" doesn't appear on Konsole. I tried to debug it in the constructor of the class, but gdb never goes there.
Thanks for your help

MyObject.h

#include <QtCore/QObject>
#include <QtCore/QTimer>
class MyObject : public QObject
{
Q_OBJECT
public:
MyObject();
private:
QTimer *timer;

private slots:
void timerEvent();
};

main.cpp

MyObject::MyObject()
{
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
timer->setInterval(10);
timer->start();
}

void MyObject::timerEvent()
{
std::cout << "Hi" << std::endl;
timer->start();
}

int main()
{
MyObject mo;
sleep(5);
return 0;
}

spirit
10th April 2009, 10:33
of course it will not work. I don't see where you start event loop.
try this.


...

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
MyObject mo;
return app.exec();
}

hosseinyounesi
10th April 2009, 10:37
Noooooooooooooooooooooooo :crying:

Sorry for such a stupid question :o

seim
10th April 2009, 19:47
And if you derive your class A from some other class B, you should call B's constructor in A():


MyObject::MyObject()
: QObject()
{
....
}

bluefly
10th April 2009, 22:51
And if you derive your class A from some other class B, you should call B's constructor in A():


MyObject::MyObject()
: QObject()
{
....
}

actually that happens implicitly. if you want to call a non default base class constructor, you have to say so explicitly using that syntax.