Important: Please read the Qt Code of Conduct -

implementing my own QTimer: never signals the slot?

  • here's my cute little timer impl:

    class QKJamsTimer : public QObject {
    	typedef QObject		_inherited;
    	QTimer			i_timer;
    	bool			i_first_timeB;
    	APP_ForkInfo		*i_forkInfoP;
    	APP_TimerForkData	*i_timerDataP;
    	QKJamsTimer(APP_ForkInfo *forkInfoP) :
    		i_timerDataP((APP_TimerForkData *)forkInfoP->dataP), 
    		Duration		delayMs(i_timerDataP->GetDelayTime() * kMiliSPerSecond);
    		connect(&i_timer, SIGNAL(timeout()), this, SLOT(timer_fired()));
    	//	time in seconds
    	void		setFireInterval(EventTimerInterval intervalF)
    		Duration		intervalMs(intervalF * kMiliSPerSecond);
    	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()
    		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 this into it, and that crashes, so i made it "has-a" instead of "is-a".

    I can't un-comment Q_OBJECT because 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 called

    what 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!

  • Lifetime Qt Champion

    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 includeQ_OBJECT?

  • Lifetime Qt Champion


    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 trick

    okay, well that sounds promising, except for:

    1. I AM using qmake
    2. i did re-run qmake
    3. 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?

  • 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!

  • Lifetime Qt Champion

    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.

Log in to reply