Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Passing arguments to QObject::connect SLOT function

    General and Desktop
    beginner
    3
    8
    5969
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • N
      Nixxer last edited by A Former User

      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!

      1 Reply Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        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?

        N 1 Reply Last reply Reply Quote 0
        • N
          Nixxer @Chris Kawa last edited by

          @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)"...

          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 0
            • Chris Kawa
              Chris Kawa Moderators last edited by

              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.

              1 Reply Last reply Reply Quote 1
              • N
                Nixxer last edited by

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

                1 Reply Last reply Reply Quote 0
                • Chris Kawa
                  Chris Kawa Moderators last edited by

                  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.

                  1 Reply Last reply Reply Quote 1
                  • N
                    Nixxer last edited by Nixxer

                    Now i have better understood the dynamics of connect.

                    Thanks for your patience.

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post