Important: Please read the Qt Code of Conduct -

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!

  • Moderators

    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 the SLOT macro is limited to (obviously, as the name suggests) the slots of that receiver class.
    There are a couple more overloads of the connect function 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)"...

  • Lifetime Qt Champion


    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 ?

  • Moderators

    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.

  • Thanks!
    So, how can I make a signal return a specific value? And then make it reach the setter() function?

    By the way, I have declared setter in the private slots of Mainwindow, in which there is the connect function...

  • Moderators

    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 signal void 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.

  • Now i have better understood the dynamics of connect.

    Thanks for your patience.

Log in to reply