Results 1 to 7 of 7

Thread: Problems with signals and slots in derived class

  1. #1
    Join Date
    May 2013
    Location
    Moscow
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with signals and slots in derived class

    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)
    Qt Code:
    1. class Worm : public QGraphicsItem
    2. {
    3. // Q_OBJECT
    4.  
    5. public:
    6. Worm();
    7. Worm (QGraphicsPathItem * argWorld);
    8. QRectF boundingRect() const;
    9. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    10. void advance(int phase);
    11.  
    12. private:
    13.  
    14. signals:
    15. void hasTouchedTheGround();
    16. };
    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.
    Qt Code:
    1. addItem(world);
    2. addItem(myworm);
    3. // let the worm fall down till collision
    4. timer = new QTimer(this);
    5. if (!myworm->collidesWithItem(world)) {
    6. QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance()));
    7. QObject::connect(myworm, SIGNAL(hasTouchedTheGround()), timer, SLOT(stop()));
    8. }
    9. timer->start(50);
    To copy to clipboard, switch view to plain text mode 
    And here comes the compile error:
    Qt Code:
    1. myscene.cpp: In constructor 'MyScene::MyScene(QObject*)':
    2. myscene.cpp:48:84: error: no matching function for call to 'MyScene::connect(Worm*&, const char*, QTimer*&, const char*)'
    3. [bla-bla-bla, candidates etc]
    4. /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:
    Qt Code:
    1. 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:
    Qt Code:
    1. moc_worm.cpp: In static member function 'static void Worm::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)':
    2. moc_worm.cpp:47:42: error: invalid static_cast from type 'QObject*' to type 'Worm*'
    3. moc_worm.cpp: At global scope:
    4. moc_worm.cpp:61:8: error: 'staticMetaObject' is not a member of 'QGraphicsItem'
    5. /usr/include/qt4/QtCore/qobject.h: In member function 'virtual const QMetaObject* Worm::metaObject() const':
    6. /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:
    Qt Code:
    1. class Worm : public QGraphicsItem, public QObject
    To copy to clipboard, switch view to plain text mode 
    qmake, rebuild. Got only 3 errors:
    Qt Code:
    1. moc_worm.cpp:61:8: error: 'staticMetaObject' is not a member of 'QGraphicsItem'
    2. moc_worm.cpp: In member function 'virtual void* Worm::qt_metacast(const char*)':
    3. moc_worm.cpp:81:12: error: 'qt_metacast' is not a member of 'QGraphicsItem'
    4. moc_worm.cpp: In member function 'virtual int Worm::qt_metacall(QMetaObject::Call, int, void**)':
    5. moc_worm.cpp:86:11: error: 'qt_metacall' is not a member of 'QGraphicsItem'
    6. moc_worm.cpp: In member function 'virtual void* Worm::qt_metacast(const char*)':
    7. moc_worm.cpp:82:1: warning: control reaches end of non-void function [-Wreturn-type]
    8. 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:
    Qt Code:
    1. 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?
    Last edited by ittod; 20th May 2013 at 13:19.

  2. #2
    Join Date
    Sep 2011
    Location
    Bangalore
    Posts
    254
    Thanks
    92
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Problems with signals and slots in derived class

    Uncomment the macro Q_OBJECT in your class Worm and rebuild your project.

  3. #3
    Join Date
    May 2013
    Location
    Moscow
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with signals and slots in derived class

    Quote Originally Posted by rawfool View Post
    Uncomment the macro Q_OBJECT in your class Worm and rebuild your project.
    I already did that. That caused 14 errors.

  4. #4
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems with signals and slots in derived class

    If you want signals and slots, you should inherit QObject. QGraphicsItem doesn't inherit it.
    In your case you should inherit QGraphicsObject, which is already inherits QGraphicsItem and QObject.

    http://qt-project.org/doc/qt-4.8/qgraphicsobject.html
    (And of course uncomment Q_OBJECT macro).

  5. The following user says thank you to lanz for this useful post:

    ittod (21st May 2013)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with signals and slots in derived class

    Quote Originally Posted by ittod View Post
    I already did that. That caused 14 errors.
    Rerun qmake.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    May 2013
    Location
    Moscow
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problems with signals and slots in derived class

    Quote Originally Posted by lanz View Post
    If you want signals and slots, you should inherit QObject. QGraphicsItem doesn't inherit it.
    In your case you should inherit QGraphicsObject, which is already inherits QGraphicsItem and QObject.

    http://qt-project.org/doc/qt-4.8/qgraphicsobject.html
    (And of course uncomment Q_OBJECT macro).
    Thanks a lot, I didn't know about that class. That's just what I needed. Also it's built-in signals helped me a lot.

    Quote Originally Posted by wysota
    Rerun qmake.
    I did that, didn't help.
    Now the problem is solved thanks to lanz. Causes of the problem:
    1) QGraphicsItem doesn't inherit QObject, so it doesn't support signals & slots.
    2) For some reason, the order of inheritance mattered. So when I wrote "class Worm : public QGraphicsItem, public QObject", the project didn't compile and returned errors. All errors disappeared just when I changed the inheritance that way: "class Worm : public QObject, public QGraphicsItem". That's really weird.

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problems with signals and slots in derived class

    Quote Originally Posted by ittod View Post
    All errors disappeared just when I changed the inheritance that way: "class Worm : public QObject, public QGraphicsItem". That's really weird.
    It's documented.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 37
    Last Post: 14th December 2012, 10:50
  2. Signals Slots and Class Pointers
    By Atomic_Sheep in forum Newbie
    Replies: 18
    Last Post: 7th September 2012, 10:08
  3. Replies: 6
    Last Post: 2nd May 2012, 10:13
  4. Problems with signals and slots
    By zzz9 in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2011, 09:52
  5. Access a class without using Signals/Slots
    By impeteperry in forum Qt Programming
    Replies: 5
    Last Post: 10th January 2010, 12:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.