Hey, everyone!
I've been searching the web about my problem and found some strange solutions, but they didn't work. Well, let me explain the situation first.
I'm using Qt Creator (2.4.1, based on Qt 4.8.0(64 bit)) on Linux Mint 13. Writing some kind of a simplest "Worms" clone, using QGraphicsView, QGraphicsScene and QGraphicsItems.
Created a class Worm, that inherits from QGraphicsItem. (I'll explain why Q_OBJECT is commented out later)
{
// Q_OBJECT
public:
Worm();
void advance(int phase);
private:
signals:
void hasTouchedTheGround();
};
class Worm : public QGraphicsItem
{
// Q_OBJECT
public:
Worm();
Worm (QGraphicsPathItem * argWorld);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void advance(int phase);
private:
QGraphicsPathItem * world;
signals:
void hasTouchedTheGround();
};
To copy to clipboard, switch view to plain text mode
I've already drawn the random "world" in my custom QGraphicsScene constructor using QGraphicsPathItem, so I wanted to add a Worm in some spot in my scene and let it fall to the ground. Simple.
addItem(world);
addItem(myworm);
// let the worm fall down till collision
if (!myworm->collidesWithItem(world)) {
QObject::connect(timer,
SIGNAL(timeout
()),
this,
SLOT(advance
()));
QObject::connect(myworm,
SIGNAL(hasTouchedTheGround
()), timer,
SLOT(stop
()));
}
timer->start(50);
addItem(world);
addItem(myworm);
// let the worm fall down till collision
timer = new QTimer(this);
if (!myworm->collidesWithItem(world)) {
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance()));
QObject::connect(myworm, SIGNAL(hasTouchedTheGround()), timer, SLOT(stop()));
}
timer->start(50);
To copy to clipboard, switch view to plain text mode
And here comes the compile error:
myscene.cpp: In constructor 'MyScene::MyScene(QObject*)':
myscene.cpp:48:84: error: no matching function for call to 'MyScene::connect(Worm*&, const char*, QTimer*&, const char*)'
[bla-bla-bla, candidates etc]
/usr/include/qt4/QtCore/qobject.h:204:17: note: no known conversion for argument 1 from 'Worm*' to 'const QObject*'
myscene.cpp: In constructor 'MyScene::MyScene(QObject*)':
myscene.cpp:48:84: error: no matching function for call to 'MyScene::connect(Worm*&, const char*, QTimer*&, const char*)'
[bla-bla-bla, candidates etc]
/usr/include/qt4/QtCore/qobject.h:204:17: note: no known conversion for argument 1 from 'Worm*' to 'const QObject*'
To copy to clipboard, switch view to plain text mode
That was a big surprise. As far as I know, QGraphicsItem has QObject somewhere among his parents.
I found a solution here. It was said, that a compiler "does not know", that my class is derived from QObject, and the solution is as simple as adding #include "worm.h" to myscene.cpp file.
Didn't work. *deep sigh* so I had to add a cast (QObject*).
Okay, then I added emission of my signal to worm.cpp. And here's the magic:
myWormApp/worm.cpp:23: undefined reference to `Worm::hasTouchedTheGround()'
myWormApp/worm.cpp:23: undefined reference to `Worm::hasTouchedTheGround()'
To copy to clipboard, switch view to plain text mode
Whoa! So, compiler still doesn't understand, that Worm is derived from QObject. Okay, I thought that a Q_OBJECT would help. So I added the macro, right-clicked on the project, ran qmake then ReBuild.
Build returned 14 (fourteen!) errors.
The output is quite long, so here's just a few lines from it:
moc_worm.cpp: In static member function 'static void Worm::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
moc_worm.cpp:47:42: error: invalid static_cast from type 'QObject*' to type 'Worm*'
moc_worm.cpp: At global scope:
moc_worm.cpp:61:8: error: 'staticMetaObject' is not a member of 'QGraphicsItem'
/usr/include/qt4/QtCore/qobject.h: In member function 'virtual const QMetaObject* Worm::metaObject() const':
/usr/include/qt4/QtCore/qobject.h:320:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
moc_worm.cpp: In static member function 'static void Worm::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
moc_worm.cpp:47:42: error: invalid static_cast from type 'QObject*' to type 'Worm*'
moc_worm.cpp: At global scope:
moc_worm.cpp:61:8: error: 'staticMetaObject' is not a member of 'QGraphicsItem'
/usr/include/qt4/QtCore/qobject.h: In member function 'virtual const QMetaObject* Worm::metaObject() const':
/usr/include/qt4/QtCore/qobject.h:320:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
To copy to clipboard, switch view to plain text mode
I've gone crazy and decided to inherit from QObject:
class Worm : public QGraphicsItem, public QObject
To copy to clipboard, switch view to plain text mode
qmake, rebuild. Got only 3 errors:
moc_worm.cpp:61:8: error: 'staticMetaObject' is not a member of 'QGraphicsItem'
moc_worm.cpp: In member function 'virtual void* Worm::qt_metacast(const char*)':
moc_worm.cpp:81:12: error: 'qt_metacast' is not a member of 'QGraphicsItem'
moc_worm.cpp: In member function 'virtual int Worm::qt_metacall(QMetaObject::Call, int, void**)':
moc_worm.cpp:86:11: error: 'qt_metacall' is not a member of 'QGraphicsItem'
moc_worm.cpp: In member function 'virtual void* Worm::qt_metacast(const char*)':
moc_worm.cpp:82:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [moc_worm.o] Error 1
moc_worm.cpp:61:8: error: 'staticMetaObject' is not a member of 'QGraphicsItem'
moc_worm.cpp: In member function 'virtual void* Worm::qt_metacast(const char*)':
moc_worm.cpp:81:12: error: 'qt_metacast' is not a member of 'QGraphicsItem'
moc_worm.cpp: In member function 'virtual int Worm::qt_metacall(QMetaObject::Call, int, void**)':
moc_worm.cpp:86:11: error: 'qt_metacall' is not a member of 'QGraphicsItem'
moc_worm.cpp: In member function 'virtual void* Worm::qt_metacast(const char*)':
moc_worm.cpp:82:1: warning: control reaches end of non-void function [-Wreturn-type]
make: *** [moc_worm.o] Error 1
To copy to clipboard, switch view to plain text mode
Is there any solution? I can't understand why nothing works, it seems so simple and clear.
Added after 6 minutes:
Sorry for double-post, I got a short update.
For some random reason I decided to change the inheritance order to:
class Worm : public QObject, public QGraphicsItem
Now compiler returned only one warning:
worm.
h:24: Warning
: Class Worm implements the interface
QGraphicsItem but does not list it in
Q_INTERFACES.
qobject_cast to
QGraphicsItem will not work
!
worm.h:24: Warning: Class Worm implements the interface QGraphicsItem but does not list it in Q_INTERFACES. qobject_cast to QGraphicsItem will not work!
To copy to clipboard, switch view to plain text mode
But still I can't understand what is happening, what do all these strange errors mean?
Bookmarks