View Full Version : Simple QTimer Question
shiranraviv
8th November 2009, 09:11
Hi,
Im tryng to user QTimer in a regular class (not inherited form any QT feature),
I want to jump to OnTimer() when timeout() is occur.
and im looking forward the connect command...
For example:
class Display
{
Init()
{
QTimer *timer = new QTimer();
timer->setInterval( 50 );
timer->start();
connect(???);
}
OnTimer()
{
//commands..
}
}
caduel
8th November 2009, 09:26
QTimer::timeout() is a signal, thus you need a slot for it to connect to.
If your class does not (somehow) derive from QObject you don't and can't have any.
So you can not connect to OnTimeout(). Solution: Do make it a QObject.
shiranraviv
8th November 2009, 12:16
OK, so i understand right - this code i just wrote need to do it
mytimer.h
************
class MYTimer : public QObject
{
public:
MYTimer(...);
private slots:
void OnTimer();
private:
QTimer *mytimer;
};
mytimer.cpp
#include "mytimer.h"
MYTimer::MYTimer(...)
{
mytimer = new QTimer();
mytimer->setInterval( 50 );
mytimer->start();
connect(mytimer,SIGNAL(timeout()),SLOT(OnTimer())) ;
}
void MYTimer::OnTimer()
{
client->iterate();
}
and from my other class it looks like this
MYTimer *timer= new MYTimer(.....);
anyway it says that "Object::connect: No such slot QObject::OnTimer() in mytimer.cpp"
Corinzio
8th November 2009, 12:25
You missed the Q_OBJECT macro:
See in the docs: Q_OBJECT (http://doc.trolltech.com/4.3/qobject.html#Q_OBJECT)
should be in private section of your .h file
shiranraviv
8th November 2009, 13:01
Oh, you right... im sorry ... stupid mistake
but that just cause even a bigger problem -
"error: collect2: ld returned 1 exit status"
with this line before it:
"undefined reference to `vtable for MYTimer'"
any idea what is going on in here?
thanks alot
shiranraviv
8th November 2009, 13:13
Thanks alot!!!
Its working!!!
ohhhhhh:D:D:D:D
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.