Passing arguments to QObject::connect SLOT function
-
Hi!
I'd like to know how can i pass arbitrary arguments to a function called in the SLOT of connect.
This is my snippet of code of the connect that i use:
QObject::connect(dialog, SIGNAL(signal()), this, SLOT(setter()));(dialog is another class with its files)
How can i pass an argument to the setter() function without the error "QObject::connect: No such slot MainWindow::setter()" ?
I have tried to search this topic on Internet, but i couldn't find anything useful (to me)...Thanks in advance!
-
What do you mean by passing arbitrary arguments to function? C++ is a statically typed language. A function always takes a defined type arguments.
When using the above
connectsyntax what you can put in theSLOTmacro is limited to (obviously, as the name suggests) the slots of that receiver class.
There are a couple more overloads of theconnectfunction that don't use macros. In these you can pass any functor (member, function or a function object like a lambda or a class/struct with operator() ) that either matches the arguments of the signal or has fewer of them.Maybe it would be easier to help if you said what you actually try to achieve?
-
What do you mean by passing arbitrary arguments to function? C++ is a statically typed language. A function always takes a defined type arguments.
When using the above
connectsyntax what you can put in theSLOTmacro is limited to (obviously, as the name suggests) the slots of that receiver class.
There are a couple more overloads of theconnectfunction that don't use macros. In these you can pass any functor (member, function or a function object like a lambda or a class/struct with operator() ) that either matches the arguments of the signal or has fewer of them.Maybe it would be easier to help if you said what you actually try to achieve?
@Chris-Kawa I'd like to make the setter function take an int argument, like a variable or a literal or even a function that returns an int. When I try to pass to setter an argument, for example 10, I get this error with connect: "QObject::connect: No such slot MainWindow::setter()", even if I have defined the slot: "void setter(int x)"...
-
Hi,
You can't pass an argument value when you connect a signal to a slot.
As for your error is setter declared as a slot ?
-
It's because it's not you that is suppose to pass an argument in a
connect. It's the signal. If you want a setter that takes an int you can only connect it to a signal that passes that int. -
So, how can I make a signal return a specific value?
Signal is a function. It takes parameters. You specify parameter values at the call site (emit).
For example if there is a signalvoid somethingHappened(int)you can emit it somewhere:emit somethingHappened(42);. At this time any slot connected to that signal will be called with value 42.