View Full Version : signal slot conection using a string, not a SLOT

5th February 2006, 04:48
Ok here is my problem. I have to connect a QAction signal to a QTextEdit slot. Here are my parameters

object *
SIGNAL (triggered ())
object *
QString (containing slot name) -- Eample: signal is void copy (), string is "copy"

How can I do this. Currently all of my attenpts have failed. I always get a false returned from the connect function.

On thing I know is this should be possible because the slots can be found via QMetaObject::invokeMethod using a string.

5th February 2006, 05:21
Hmm... doesn't SLOT(somestring) work? If it doesn't (and it probably doesn't), then take a look at Qt source code and find how the SLOT macro expands and modify the string to be the same.

5th February 2006, 15:46
Where can I find this macro definition. I have looked all over the source code and can't find this definition.

5th February 2006, 15:50

#define SLOT(a) "1"#a
#define SIGNAL(a) "2"#a

So in your case, just prepend the slot signature with "1".

5th February 2006, 17:15
WOW... it works great.

I had to adjust my string to be "1copy ()" instead of "copy", and it works. I wonder why they add the 1 to the end of this? Seems this makes things a bit more difficult. I'm there is a reason.

5th February 2006, 17:43
It's an easy way to distinguish between methods ("0"), slots ("1"), signals ("2") and garbage (??).

5th February 2006, 19:52
They prepend a digit because in C++ no identifiers like method or variable names may start with a digit. That way you can distinguish between the types like wysota mentioned.