implementing my own QTimer: never signals the slot?
-
here's my cute little timer impl:
class QKJamsTimer : public QObject { typedef QObject _inherited; //Q_OBJECT QTimer i_timer; bool i_first_timeB; APP_ForkInfo *i_forkInfoP; APP_TimerForkData *i_timerDataP; public: QKJamsTimer(APP_ForkInfo *forkInfoP) : i_timer(this), i_first_timeB(true), i_timerDataP((APP_TimerForkData *)forkInfoP->dataP), i_forkInfoP(forkInfoP) { Duration delayMs(i_timerDataP->GetDelayTime() * kMiliSPerSecond); connect(&i_timer, SIGNAL(timeout()), this, SLOT(timer_fired())); i_timer.start(delayMs); } // time in seconds void setFireInterval(EventTimerInterval intervalF) { Duration intervalMs(intervalF * kMiliSPerSecond); i_timer.setInterval(intervalMs); } public Q_SLOTS: void timer_fired() { OSErr err = noErr; if (i_first_timeB) { i_first_timeB = false; /* we can't separately set the delay time and the fire time, so the FIRST fire time is the delay time, and subsequent fire times are regular ones. */ // note that i_timerDataP->SetFireInterval() just calls back into this->setFireInterval() i_timerDataP->SetFireInterval(i_timerDataP->GetFireInterval()); } XTE(err = i_forkInfoP->thiz->CB_Fork(i_forkInfoP)); PostThreadError("Fork Timer Error", err); } };I couldn't make my object derive from QTimer itself, since you have to pass
thisinto it, and that crashes, so i made it "has-a" instead of "is-a".I can't un-comment
Q_OBJECTbecause then my app won't link due to this:"vtable for QKJamsTimer", referenced from: QKJamsTimer::QKJamsTimer(APP_ForkInfo*) in CThreads.o NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.but all funcs ARE inline so.... ¯\_(ツ)_/¯
the deal is:
1: i can't set breakpoints into thetimer_fired()function, they just disappear and go down to the next line of executable code, fa below this class and
2: thetimer_fired()slot is never calledwhat am i missing?
-
that was it!
i put the class defs into the header, re-ran
qmake, and now all is well. thanks for the hint! -
You need Q_OBJECT if you want to use old style signal/slots ... you will see a runtime warning that the connect failed (and connect will also return false)...
-
are there ... new style signals / slots? if so, how?
or, how do i get past the link error mentioned above when i attempt to include
Q_OBJECT? -
Hi,
The Signals & Slots chapter of QT's documentation explains that.
Did you check the value of the interval you set ?
-
@SGaist i hope they pay you a full time salary, you are the most awesome helpful person i've ever met.
yes, the value is correct. okay so according to the doc:
We recommend using the qmake makefile generation tool for building your makefiles. This tool generates a makefile that does all the necessary moc handling... If you use qmake, try rerunning it to update your makefile. This should do the trickokay, well that sounds promising, except for:
- I AM using
qmake - i did re-run
qmake - same problem
it mentions editing the make file but won't that just get over-written next time i run qmake? do i need to do anything special BESIDES run qmake? do i need to, in my .pro file, mark this file as requiring moc then include the moc-generated file?
- I AM using
-
TIDBIT: this class is in a C++ file. i think i must inform my .pro file somehow to run moc over it??
-
that was it!
i put the class defs into the header, re-ran
qmake, and now all is well. thanks for the hint! -
If you define classes in .cpp files you have to add
#include "name_of_file.moc"for qmake to find that moc has to process that file to find said classe there.