Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing arguments to QObject::connect SLOT function
QtWS25 Last Chance

Passing arguments to QObject::connect SLOT function

Scheduled Pinned Locked Moved Solved General and Desktop
beginner
8 Posts 3 Posters 6.5k Views
  • 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 Offline
    N Offline
    Nixxer
    wrote on last edited by A Former User
    #1

    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
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      0
      • Chris KawaC Chris Kawa

        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 Offline
        N Offline
        Nixxer
        wrote on last edited by
        #3

        @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
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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
            1
            • N Offline
              N Offline
              Nixxer
              wrote on last edited by
              #6

              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
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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
                1
                • N Offline
                  N Offline
                  Nixxer
                  wrote on last edited by Nixxer
                  #8

                  Now i have better understood the dynamics of connect.

                  Thanks for your patience.

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved