Results 1 to 12 of 12

Thread: signal/slot problems with inheritance

  1. #1
    Join Date
    Sep 2009
    Posts
    64

    Default signal/slot problems with inheritance

    Im having some problems connecting a signal to a slot in a 2 class, inheritance type structure.

    Basically, I have a base class that is waiting for some data to change, once the data gets updated it will send a signal. I have a receiver class (it inherites from the base class) that is supposed to grab the signal and do some processing on the updated data. I have no compile or runtime errors regarding missing slots or unregistered metatypes. Also, I checked the return value of the connect call and it is succeeding. I will post some code to give a better idea of what is going on, but i feel like i have tried everything.

    base class header
    Qt Code:
    1. #include <QObject>
    2.  
    3. class UIBase : public QObject, public Switch::Subscriber
    4. {
    5. Q_OBJECT
    6. public:
    7. UIBase();
    8.  
    9. void onData(const std::string&);
    10.  
    11. signals:
    12. void onNewData();
    13. };
    To copy to clipboard, switch view to plain text mode 

    base class cpp file
    Qt Code:
    1. #include "ui-base.h"
    2. #include <QDebug>
    3. #include <QMetaType>
    4.  
    5. UIBase::UIBase()
    6. : QObject()
    7. {
    8. }
    9.  
    10. void UIBase::onData(const std::string &typeID)
    11. {
    12. emit onNewData();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Receiver class header
    Qt Code:
    1. class Receiver : public UIBase
    2. {
    3. Q_OBJECT
    4. public:
    5. Receiver();
    6.  
    7. public slots:
    8. void onReceiveData();
    9. };
    To copy to clipboard, switch view to plain text mode 

    Receiver class cpp
    Qt Code:
    1. Receiver::Receiver()
    2. {
    3. if (connect(this, SIGNAL(onNewData()), this, SLOT(onReceiveData())))
    4. qDebug() << "SIGNAL/SLOT CONNECTION SUCCESS!";
    5. else
    6. qDebug() << "SIGNAL/SLOT FAILED!";
    7. }
    8.  
    9. void Receiver::onReceiveData()
    10. {
    11. qDebug() << "SLOT CALLED";
    12. }
    To copy to clipboard, switch view to plain text mode 


    Any and all help/suggestions will be greatly appreciated

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot problems with inheritance

    Does the onData(std::string) function get called?


    Added after 5 minutes:


    Aside from that, why not make onData virtual and override it in your Receiver class?
    Last edited by franz; 7th December 2010 at 06:11.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: signal/slot problems with inheritance

    I think you connect the slot onReceiveData of the receiver object to the onNewData signal of the receiver object itself and not the signal of the base object.

    Signals and slots are connected between objects (instantiations of a class), and not between 'classes'. You should pass your receiver object a pointer to the base object, so it can connect its slots the signals of the base object.

    Regards,
    Marc

  4. #4
    Join Date
    Sep 2009
    Posts
    64

    Default Re: signal/slot problems with inheritance

    @franz: yes, that onData function is being called, and yes, i could make the function virtual... but thats not really how i am trying to design this project. there will be multiple signal/slot connections and I dont want to force any receiver classes to have to implement and override the virtual function in the base class.

    @marcvanriet: that is a good suggestion, but I do believe this type of signal/slot connection works. when an instance of the baseclass is created, so is an instance of the receiver class. 'this' object should include both the receiver class and any class it inherites from. otherwise, i should be seeing some compile/runtime error telling me it doesnt know what signal i am using to connect the slot.

  5. #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: signal/slot problems with inheritance

    Your connect statement tries to make a connection between a signal and a slot which do not exist. Check the spelling and argument list.
    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.


  6. #6
    Join Date
    Sep 2009
    Posts
    64

    Default Re: signal/slot problems with inheritance

    Quote Originally Posted by wysota View Post
    Your connect statement tries to make a connection between a signal and a slot which do not exist. Check the spelling and argument list.
    I dont see the spelling error of which you speak. Wouldnt I get a compiler error or at least a runtime error message informing me of the unsuccessful connection? I am seeing no compile errors, and no runtime error messages. there was a point in this development that I DID see runtime errors informing me that the signal was non-existent... this is no longer the case. I dont think it is a spelling error

  7. #7
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot problems with inheritance

    What is the Switch::Subscriber exactly?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  8. #8
    Join Date
    Sep 2009
    Posts
    64

    Default Re: signal/slot problems with inheritance

    it is another class of mine. really doesnt do anything as of now. it shouldnt affect Qt at all. basically just an empty class with a struct definition.

  9. #9
    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: signal/slot problems with inheritance

    Quote Originally Posted by Slewman View Post
    I dont see the spelling error of which you speak.
    Sorry, my bad. I didn't pay enough attention to your code.

    Wouldnt I get a compiler error or at least a runtime error message informing me of the unsuccessful connection? I am seeing no compile errors, and no runtime error messages.
    You wouldn't get any compilation erros. You would get runtime errors provided you had console support enabled.

    Are you using threads in your application?
    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.


  10. #10
    Join Date
    Sep 2009
    Posts
    64

    Default Re: signal/slot problems with inheritance

    Haha no worries man. I actually just found the issue... I was calling exec on my QApplication too late, i knew it was something stupid like that.

    Thanks everyone for taking the time!

  11. #11
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: signal/slot problems with inheritance

    Ah, that part was not in your example code...
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  12. #12
    Join Date
    Sep 2009
    Posts
    64

    Default Re: signal/slot problems with inheritance

    yea... i didnt post my main.cpp file, i just didnt think the problem would be in there... guess i was wrong haha.

Similar Threads

  1. Replies: 8
    Last Post: 13th July 2010, 14:08
  2. Signal connected to slot (or signal)
    By Althor in forum Newbie
    Replies: 2
    Last Post: 6th July 2010, 10:00
  3. Newbie signal-slot problems in a tcp server-client app
    By neoclaw in forum Qt Programming
    Replies: 2
    Last Post: 31st May 2010, 10:00
  4. Inheritance and signal
    By !Ci in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2007, 11:11
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.