Results 1 to 3 of 3

Thread: Problem with Signal/Slot connections in derived classes

  1. #1
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with Signal/Slot connections in derived classes

    I have a problem with Signal/Slot connections when a slot exists in a derived class, but not the baseclass. Imagine the following code, where class A is derived from QObject, and class B is derived from class A.

    Class A:
    Qt Code:
    1. #include <QObject>
    2.  
    3. class A : public QObject
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. A(QObject *parent = NULL);
    9. virtual ~A() {};
    10. };
    11.  
    12. A::A(QObject *parent) : QObject(parent)
    13. {
    14. }
    To copy to clipboard, switch view to plain text mode 

    Class B:
    Qt Code:
    1. #include "A.h"
    2.  
    3. class B : public A
    4. {
    5. public:
    6. B(QObject *parent = NULL);
    7. virtual ~B() {};
    8.  
    9. public slots:
    10. void mySlot();
    11. };
    12.  
    13. B::B(QObject *parent) : A(parent)
    14. {
    15. }
    16.  
    17. void B::mySlot()
    18. {
    19. qDebug("B::mySlot called");
    20. }
    To copy to clipboard, switch view to plain text mode 

    Now I want to create an instance of class B, and connect the slot defined in class B (but not in class A!) to a signal.

    Qt Code:
    1. QApplication app(argc, argv);
    2. B b;
    3. QObject::connect(&app, SIGNAL(aboutToQuit()), &b, SLOT(mySlot()));
    To copy to clipboard, switch view to plain text mode 

    Instead of the slot being called, I get the following warning in the debug output:
    Qt Code:
    1. WARNING: Object::connect: No such slot A::mySlot()
    2. WARNING: Object::connect: (sender name: 'tst_Qt')
    To copy to clipboard, switch view to plain text mode 

    Why does it try to connect to A::mySlot()? b is clearly an instance of B, not of A.
    Qt is version 4.3.1. I'd be very happy about any hint to solve this problem..

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Signal/Slot connections in derived classes

    Add the Q_OBJECT macro to B also.

  3. The following user says thank you to marcel for this useful post:

    hb (20th September 2007)

  4. #3
    Join Date
    May 2007
    Posts
    19
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with Signal/Slot connections in derived classes

    Ouch. That was really a stupid mistake. Thanks for your help!

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.