PDA

View Full Version : signal/slot problems with inheritance



Slewman
7th December 2010, 00:11
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


#include <QObject>

class UIBase : public QObject, public Switch::Subscriber
{
Q_OBJECT
public:
UIBase();

void onData(const std::string&);

signals:
void onNewData();
};


base class cpp file


#include "ui-base.h"
#include <QDebug>
#include <QMetaType>

UIBase::UIBase()
: QObject()
{
}

void UIBase::onData(const std::string &typeID)
{
emit onNewData();
}


Receiver class header


class Receiver : public UIBase
{
Q_OBJECT
public:
Receiver();

public slots:
void onReceiveData();
};



Receiver class cpp


Receiver::Receiver()
{
if (connect(this, SIGNAL(onNewData()), this, SLOT(onReceiveData())))
qDebug() << "SIGNAL/SLOT CONNECTION SUCCESS!";
else
qDebug() << "SIGNAL/SLOT FAILED!";
}

void Receiver::onReceiveData()
{
qDebug() << "SLOT CALLED";
}



Any and all help/suggestions will be greatly appreciated

franz
7th December 2010, 07:11
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?

marcvanriet
7th December 2010, 13:17
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

Slewman
7th December 2010, 14:45
@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.

wysota
7th December 2010, 15:18
Your connect statement tries to make a connection between a signal and a slot which do not exist. Check the spelling and argument list.

Slewman
7th December 2010, 15:25
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

franz
7th December 2010, 15:25
What is the Switch::Subscriber exactly?

Slewman
7th December 2010, 15:27
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.

wysota
7th December 2010, 16:11
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?

Slewman
7th December 2010, 16:13
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!

franz
7th December 2010, 16:18
Ah, that part was not in your example code...

Slewman
7th December 2010, 16:19
yea... i didnt post my main.cpp file, i just didnt think the problem would be in there... guess i was wrong haha.