PDA

View Full Version : signal/slot with std::string. How?



dpn
1st May 2013, 13:45
Hi,

I am using signal/slot in existing code. It's working fine. Now, I have to pass std::string as a argument to these signal/slot, this slot again emit signal, another slot..etc. (chain)

I searched in google. I tried with Q_DECLARE_METATYPE (std::string) and qRegisterMetaType<std::string>("std::string"). No luck.

How to send std::string as an argument of signal and receive in slot?

Regards,

amleto
1st May 2013, 13:54
Q_DECLARE_METATYPE (std::string) AND qRegisterMetaType<std::string>("std::string") is fine. You must be doing something wrong.

read my sig.

dpn
1st May 2013, 14:09
Thank you for your prompt response.

It's working with 3 objects of 3 different classes.

c1, c2, c3 are classes.

c1 obj1; //object1
c2 obj2; //object2
c3 obj3;//object3



now, obj3 just emits signal1. obj2 has slot1(for signal1) and emits signal2. obj1 has slot2(for signal2).

I have added Q_DECLARE_METATYPE (std::string) in c3.h file. And, in c3's constructor, Added qRegisterMetaType<std::string>("std::string").

obj3 emits signal successfully. But, slot1 on obj2 is not firing. This is the issue.

Am I doing wrong?

wysota
1st May 2013, 16:52
Are you getting any errors (e.g. runtime warnings on the console)? Basic signals/slots doesn't even need Q_DECLARE_METATYPE. What do your connect statements look like?

dpn
3rd May 2013, 12:13
Yes. Getting waring on console. QObject::connect cannot connect...

connect(obj3, SIGNAL(signalC3(std::string)), obj2, SLOT(slotC2(slotC2(std::string))); in c2 constructor.

amleto
3rd May 2013, 13:57
please post complete compilable example (read my sig)

d_stranz
3rd May 2013, 19:37
connect(obj3, SIGNAL(signalC3(std::string)), obj2, SLOT(slotC2(slotC2(std::string)));

This is nonsense and in fact won't even compile as written (missing a closing parenthesis). What the heck is "SLOT( slotc2( slotc2( std:: string ) ) )" supposed to mean anyway?

dpn
5th May 2013, 08:48
Can't you understand signalC3 emits on obj3 and slotC2 avail on obj2? Is it nonsense?

Hi Amelto,

I will try to explain this issue with sample application.

Thanks Amelto.

amleto
5th May 2013, 14:07
connect(obj3, SIGNAL(signalC3(std::string)), obj2, SLOT(slotC2(slotC2(std::string)));
yes, that doesn't make sense.

d_stranz
5th May 2013, 19:53
Can't you understand signalC3 emits on obj3 and slotC2 avail on obj2? Is it nonsense?


The nonsense is that you have a typo in your connect statement. That's why it isn't working.