Monday, 15 June 2015

c++ - Error with QObject::connect() -



c++ - Error with QObject::connect() -

i trying run qtimer , have warn me when timeouting. so, utilize slot , signal link two.

the guy.h:

#ifndef guy_h #define guy_h #include <qgraphicsitem> #include <qtimer> #include <qobject> class guy : public qgraphicsitem { public: guy(int x, int y); void timerstart(); public slots: void ontimeouttimer(); [...] qtimer *timer; } #endif // guy_h

the guy.cpp:

#include "guy.h" #include <qtimer> #include <qobject> #include <stdio.h> #include <iostream> guy::guy(int x, int y) { timer = new qtimer(); } void guy::timerstart() { qobject::connect(timer, signal(timeout()), this, slot(ontimeouttimer())); this->timer->setinterval(1000); this->timer->start(); std::cout << "starting timer" << std::endl; } void guy::ontimeouttimer() { std::cout << "check" << std::endl; }

but ouput, error:

no matching function phone call 'qobject::connect(qtimer*&, const char*, guy* const, const char*)'

as undertsand qtimer no qobject required first input of function connect(), documentation specifies qtimer inherits qobject. have no clue here.

you need inherit qobject, too, working signals , slots availabl qobjects. qgraphicsitem not inherit qobject, not indirectly.

not that, need add together q_object macro follows:

class guy : public qobject, public qgraphicsitem { q_object ... }

or improve because qgraphicsobject inherits qobject , qgraphicsitem.

... #include <qgraphicsobject> ... class guy : public qgraphicsqobject { q_object ... }

also, if create change, suggest alter qobject::connect connect not need indicate qobject:: scope then.

on side note, including stdio.h not seem create sense here.

furthermore, allocating qtimer instance on heap looks wasteful me. not leaking memory, adds additional complexity. if allocate on heap, should pass parent , utilize initializer list or c++11 style initialization. also, if allocate on heap, utilize forwards declaration in header.

if slot not used outside class, should create private.

it bad thought create timer fellow member public. not that.

c++ qt qtcore qobject qt-signals

No comments:

Post a Comment