Results 1 to 8 of 8

Thread: [newbie] signal/slot with sublassing problem

  1. #1
    Join Date
    Aug 2006
    Posts
    8
    Thanks
    2

    Default [newbie] signal/slot with sublassing problem

    Hi everyone, this is probably a newbie question, but I couldn't readily find an answer so would like to ask the group for help.

    I have a class hierarchy like the following:
    Qt Code:
    1. namespace A {
    2. class GenericService {
    3. public :
    4. void sendQuery();
    5.  
    6. protected slot:
    7. void receiveData(QByteArray &data);
    8. }
    9. }
    10.  
    11. void GenericService::sendQuery()
    12. {
    13. /* do a bunch of stuff that results in a query being sent through RemoteService */
    14.  
    15. connect (RemoteService, SIGNAL(data(QByteArray)), this, SLOT(receiveData(QByteArray));
    16.  
    17. }
    18.  
    19. namespace B {
    20. class SomeServiceType : public GenericService {
    21. public :
    22. /* some specialized functions */
    23. }
    24.  
    25. class MoreSpecificServiceType : public SomeServiceType {
    26. public :
    27. /* more specialized functions */
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    My problem is that when I execute a query from an instance of MoreSpecificServiceType the connect fails to find the slot B::SomeServiceType::receiveData unless I specifically create that function, which then hides the default behavior of A::GenericService::receiveData which is what I want to have actually handle this signal. I'd have thought that the specialized classes would inherit the appropriate slot from the original base class and I'd not need to reimplement it. Can anyone let me know what the correct approach here is? or have I just made a general mistake someone can point out?

    many thanks for your help
    Nathan

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [newbie] signal/slot with sublassing problem

    The issue is probably that you need to explicitly qualify the namespace in the SIGNAL/SLOT signatures in your connect call.
    Read http://lists.trolltech.com/qt-intere...ad01111-0.html

    If that does not help, please post the exact error messages you are getting.

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

    borker (16th April 2009)

  4. #3
    Join Date
    Aug 2006
    Posts
    8
    Thanks
    2

    Default Re: [newbie] signal/slot with sublassing problem

    Thanks for the reply and the link. I'll have a more thorough go through tonight when I get home from work and also post the exact code and message and maybe build a simple test case app if the problem is still happening.

    My first thought though is that how can I supply a namespace in a base class that won't know what namespace any future subclasses are in. In my example above, how could the connect in the base class in namespace A know what namespace the derived class in namespace B has?

    thanks
    N

  5. #4
    Join Date
    Aug 2006
    Posts
    8
    Thanks
    2

    Default Re: [newbie] signal/slot with sublassing problem

    hmmm.... I'm making things worse it seems

    I have 4 files for a test app:

    sender.h:
    Qt Code:
    1. #include <QApplication>
    2.  
    3. class Sender : public QObject {
    4. public :
    5. Sender(QObject *parent =0) : QObject(parent) {}
    6.  
    7. void pokeMe() {emit sendData();}
    8.  
    9. signals :
    10. void sendData() {}
    11. };
    To copy to clipboard, switch view to plain text mode 

    genericservice.h
    Qt Code:
    1. #ifndef GS_H
    2. #define GS_H
    3.  
    4. #include <QApplication>
    5. #include <QDebug>
    6. #include "sender.h"
    7.  
    8. namespace A {
    9. class GenericService : public QObject {
    10. public :
    11. GenericService(QObject *parent=0);
    12.  
    13. void sendQuery();
    14.  
    15. protected slots:
    16. void receiveData();
    17.  
    18. private :
    19. Sender *m_sender;
    20. };
    21. }
    22.  
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    genericservice.cpp
    Qt Code:
    1. #include "genericservice.h"
    2.  
    3. using namespace A;
    4.  
    5. GenericService::GenericService (QObject *parent) : QObject(parent)
    6. {
    7. m_sender = new Sender(this);
    8. }
    9.  
    10. void GenericService::sendQuery()
    11. {
    12. connect(m_sender, SIGNAL(sendData()), this, SLOT(receiveData()));
    13. m_sender->pokeMe();
    14. }
    15.  
    16. void GenericService::receiveData()
    17. {
    18. QDebug(new QString("yo"));
    19. }
    To copy to clipboard, switch view to plain text mode 

    and main.cpp
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "genericservice.cpp"
    4.  
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. GenericService *gen = new GenericService();
    11. gen->sendQuery();
    12.  
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    and now when I build and execute I get:

    Object::connect: No such signal QObject::sendData() in genericservice.cpp:13

    i.e. the signal from a non-namespaced class is not not being recognized in the namespaced one. I didn't even get around to the derived classes in different namespaces! Could really use some help on this!

  6. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: [newbie] signal/slot with sublassing problem

    you forgot the Q_OBJECT macro. It should look like this:
    Qt Code:
    1. class Sender : public QObject {
    2. Q_OBJECT
    3. public :
    4. Sender(QObject *parent =0) : QObject(parent) {}
    5. /* . . . */
    6. };
    To copy to clipboard, switch view to plain text mode 
    without this macro the MOC (Meta Object Compiler) don't make any interest to your class and that's bad, bacause the meta object system is the thing making signal/slots working.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. The following user says thank you to faldzip for this useful post:

    borker (17th April 2009)

  8. #6
    Join Date
    Aug 2006
    Posts
    8
    Thanks
    2

    Default Re: [newbie] signal/slot with sublassing problem

    ok, the files above don't work because I'm an idiot ^W^W^W I forgot the Q_OBJECT macro...

  9. #7
    Join Date
    Aug 2006
    Posts
    8
    Thanks
    2

    Default Re: [newbie] signal/slot with sublassing problem

    Quote Originally Posted by faldżip View Post
    you forgot the Q_OBJECT macro. It should look like this:
    Qt Code:
    1. class Sender : public QObject {
    2. Q_OBJECT
    3. public :
    4. Sender(QObject *parent =0) : QObject(parent) {}
    5. /* . . . */
    6. };
    To copy to clipboard, switch view to plain text mode 
    without this macro the MOC (Meta Object Compiler) don't make any interest to your class and that's bad, bacause the meta object system is the thing making signal/slots working.
    wow, you posted just before me! just made the change and of course everything went much better... now checking my real code for the same mistake.

    cheers
    N

  10. #8
    Join Date
    Aug 2006
    Posts
    8
    Thanks
    2

    Default Re: [newbie] signal/slot with sublassing problem

    Yup... it was a forgotten Q_OBJECT macro. Thanks for the help, I guess I can chalk this one up to experience.

    thanks
    Nathan

Similar Threads

  1. QButtonGroup buttonClicked signal/slot problem
    By davidthewatson in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2009, 17:24
  2. Replies: 16
    Last Post: 28th October 2008, 22:00
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Replies: 2
    Last Post: 20th September 2007, 12:27
  5. problem with signal/slot
    By ihoss in forum Newbie
    Replies: 2
    Last Post: 24th August 2007, 22:59

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.