Results 1 to 5 of 5

Thread: Connecting to a base class signal?

  1. #1
    Join Date
    Sep 2007
    Posts
    14
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Connecting to a base class signal?

    I am trying to connect to a signal in a base class. The pointer is the type of the base class but points to an object of the derived class. My classes are declared similar to the following:

    Qt Code:
    1. class Base : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. Base();
    6. signals:
    7. void BaseSignal(char Arg);
    8. };
    9.  
    10. class DerivA : public Base
    11. {
    12. Q_OBJECT
    13. public:
    14. DerivA();
    15.  
    16. void Alert() { emit BaseSignal('A') };
    17. };
    18.  
    19. class DerivB : public Base
    20. {
    21. Q_OBJECT
    22. public:
    23. DerivB();
    24.  
    25. void Alert() { emit BaseSignal('B') };
    26. };
    To copy to clipboard, switch view to plain text mode 

    Connect fails if I pass a DerivA or DerivB pointer to a function like the following:

    Qt Code:
    1. bool SomeObject::mbrConnect(Base *B)
    2. {
    3. return QObject::connect(B, SIGNAL(BaseSignal(char)), this, SLOT(Handler(char));
    4. }
    To copy to clipboard, switch view to plain text mode 

    It seems it is too smart for its own good, being able to identify the real type of the Base pointer in the Connect Fail output message. For example, passing mbrConnect a DerivA pointer would output the following:

    Object::connect: No such signal DerivA::BaseSignal(char)
    How do I solve this?

  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: Connecting to a base class signal?

    This happens because of moc. Only the metaobject of Base contains the implementation of the signal.
    Since the declarations of DerivA and B do not declare any signals, their metaobjects do not implement the signal, nor do they inherit from the Base metaobject.

    Solution: add the signal declaration to both derived classes and remove it from the base class.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to a base class signal?

    Quote Originally Posted by AaronMK View Post
    I am trying to connect to a signal in a base class.[...]
    How do I solve this?
    It should work. Make sure that moc is run on each of these classes and try recompiling your application.

    Quote Originally Posted by marcel View Post
    Since the declarations of DerivA and B do not declare any signals, their metaobjects do not implement the signal, nor do they inherit from the Base metaobject.
    But still meta objects of derived classes have access to meta object of the base class:
    Qt Code:
    1. const QMetaObject DerivB::staticMetaObject = {
    2. { /* --> */ &Base::staticMetaObject /* <-- */, qt_meta_stringdata_DerivB,
    3. qt_meta_data_DerivB, 0 }
    4. };
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Connecting to a base class signal?

    Quote Originally Posted by marcel View Post
    Solution: add the signal declaration to both derived classes and remove it from the base class.
    Nah, something else must be wrong. One can fairly well emit signals declared in base classes. One can also establish signal-slot connections by simply passing a plain QObject pointer, it doesn't have to be of that type declaring such signal or slot.
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Connecting to a base class signal?

    Quote Originally Posted by jpn View Post
    Nah, something else must be wrong.
    Yes, maybe a typo in signal name?

Similar Threads

  1. creating treeWidgetItem using emitted signal from thread class
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 09:37
  2. Connecting signal to form that is closing
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2007, 08:54
  3. Replies: 2
    Last Post: 17th May 2006, 22:01
  4. Signal/slot looking in base class, not derived class
    By georgie in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2006, 08:36
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26

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.