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. Argument in connect

Argument in connect

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 4 Posters 12.4k Views 3 Watching
  • 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.
  • martial123M Offline
    martial123M Offline
    martial123
    wrote on last edited by
    #1

    Hi erveryone,
    Is it possible to pass an argument (or many...) to slot when we use connect to execute this slot ??

    Ty

    Pl45m4P 1 Reply Last reply
    1
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      No it's not, you are connecting functions not executing them.

      What exactly are you trying to do ?

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

      martial123M 1 Reply Last reply
      1
      • martial123M martial123

        Hi erveryone,
        Is it possible to pass an argument (or many...) to slot when we use connect to execute this slot ??

        Ty

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @martial123

        If you are talking about "arguments" like QStrings, it is possible. You can pass a string to a slot function

        // New sytax:
        connect(mySender, &Sender::signal, receiver, &Receiver::function);
        // Signal may look like this in header:
        signals:
        void mySignal(QString s);
        
        // Or in "old" style
        connect(sender, SIGNAL(signal(QString)), receiver, SLOT(function(QString)));
        // Signal same as above
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        martial123M 1 Reply Last reply
        2
        • Pl45m4P Pl45m4

          @martial123

          If you are talking about "arguments" like QStrings, it is possible. You can pass a string to a slot function

          // New sytax:
          connect(mySender, &Sender::signal, receiver, &Receiver::function);
          // Signal may look like this in header:
          signals:
          void mySignal(QString s);
          
          // Or in "old" style
          connect(sender, SIGNAL(signal(QString)), receiver, SLOT(function(QString)));
          // Signal same as above
          
          martial123M Offline
          martial123M Offline
          martial123
          wrote on last edited by
          #4

          @Pl45m4

          hi ,
          In the new syntax, where i send for the slot the QString ??
          I need to do that ?:

          // New sytax:
          connect(mySender, &Sender::signal, receiver, &Receiver::function( QString s);
          // Signal may look like this in header:
          signals:
          void mySignal(QString s);
          
          
          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            No it's not, you are connecting functions not executing them.

            What exactly are you trying to do ?

            martial123M Offline
            martial123M Offline
            martial123
            wrote on last edited by
            #5

            @SGaist
            I just want to know if is it possible because if it's not, it's limited as system.

            If in future i need to send an object, or variable int or QString, to a slot, how i can do that ?

            mrjjM 1 Reply Last reply
            0
            • martial123M martial123

              @SGaist
              I just want to know if is it possible because if it's not, it's limited as system.

              If in future i need to send an object, or variable int or QString, to a slot, how i can do that ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @martial123
              Hi
              Yes its possible.
              as @Pl45m4 shows.
              When you use the signal, you give it the parameters values.
              emit mySignal("this is the string");
              so if we had
              void mySignal2(QString s, int v1, int v2);
              you would do
              emit mySignal("this is the string",100,200);

              You can have any number of parameters in your signals, including custom types.

              martial123M 1 Reply Last reply
              2
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                All the answers you are looking for are shown in the Signals & Slots chapter of Qt's documentation.

                Don't mix the connection "action" with the signals and slots parameters. While related they are distinct.

                Signals and slots can have parameters. And you can connect compatible signals and slots.

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

                martial123M 1 Reply Last reply
                1
                • mrjjM mrjj

                  @martial123
                  Hi
                  Yes its possible.
                  as @Pl45m4 shows.
                  When you use the signal, you give it the parameters values.
                  emit mySignal("this is the string");
                  so if we had
                  void mySignal2(QString s, int v1, int v2);
                  you would do
                  emit mySignal("this is the string",100,200);

                  You can have any number of parameters in your signals, including custom types.

                  martial123M Offline
                  martial123M Offline
                  martial123
                  wrote on last edited by martial123
                  #8

                  @mrjj
                  so after emit signal with parametres,
                  the slot is call by a classique connect but the definiton of slot is like this :

                  void mySignal2(QString s, int v1, int v2);
                  ...
                  emit mySignal2("the string", 100, 50)
                  ...
                  connect(mySender, &Sender::mySignal2, receiver, &Receiver::slot);
                  
                  void slot(QString s, int v1, int v2){
                  
                  }
                  

                  is that ?
                  ty

                  mrjjM 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    All the answers you are looking for are shown in the Signals & Slots chapter of Qt's documentation.

                    Don't mix the connection "action" with the signals and slots parameters. While related they are distinct.

                    Signals and slots can have parameters. And you can connect compatible signals and slots.

                    martial123M Offline
                    martial123M Offline
                    martial123
                    wrote on last edited by
                    #9

                    @SGaist
                    my question may be wrong, I had my answers thank you

                    1 Reply Last reply
                    0
                    • martial123M martial123

                      @mrjj
                      so after emit signal with parametres,
                      the slot is call by a classique connect but the definiton of slot is like this :

                      void mySignal2(QString s, int v1, int v2);
                      ...
                      emit mySignal2("the string", 100, 50)
                      ...
                      connect(mySender, &Sender::mySignal2, receiver, &Receiver::slot);
                      
                      void slot(QString s, int v1, int v2){
                      
                      }
                      

                      is that ?
                      ty

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @martial123
                      Yes, that's it.
                      The signal and slot must match in parameters.
                      So you could not say hook up buttons clicked signal to
                      void slot(QString s, int v1, int v2)
                      as the button would have no way of adding the string and ints.
                      However for your own signals and slots. it is of cause allowed.

                      martial123M 1 Reply Last reply
                      1
                      • mrjjM mrjj

                        @martial123
                        Yes, that's it.
                        The signal and slot must match in parameters.
                        So you could not say hook up buttons clicked signal to
                        void slot(QString s, int v1, int v2)
                        as the button would have no way of adding the string and ints.
                        However for your own signals and slots. it is of cause allowed.

                        martial123M Offline
                        martial123M Offline
                        martial123
                        wrote on last edited by martial123
                        #11

                        @mrjj
                        ok added
                        ty

                        if i need to send parametres with signal by clicked for exemple,
                        i do this:

                        in constructor :
                        connect::(QPushButton, &QPushButton::clicked, this, &class::slot)
                        
                        
                        void slot ()
                        {
                        emit mySignal2("the string", 100, 50)
                        }
                        
                         // in main i receive this signal perso and i can connect to a other slot with parametre 
                        
                        mrjjM 1 Reply Last reply
                        0
                        • martial123M martial123

                          @mrjj
                          ok added
                          ty

                          if i need to send parametres with signal by clicked for exemple,
                          i do this:

                          in constructor :
                          connect::(QPushButton, &QPushButton::clicked, this, &class::slot)
                          
                          
                          void slot ()
                          {
                          emit mySignal2("the string", 100, 50)
                          }
                          
                           // in main i receive this signal perso and i can connect to a other slot with parametre 
                          
                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @martial123
                          Yes.
                          You can also use lambdas to avoid creating a slot for that.

                          connect(QPushButton, &QPushButton::clicked, this, [](){
                          emit mySignal2("the string", 100, 50);
                          });
                          
                          
                          martial123M 1 Reply Last reply
                          1
                          • mrjjM mrjj

                            @martial123
                            Yes.
                            You can also use lambdas to avoid creating a slot for that.

                            connect(QPushButton, &QPushButton::clicked, this, [](){
                            emit mySignal2("the string", 100, 50);
                            });
                            
                            
                            martial123M Offline
                            martial123M Offline
                            martial123
                            wrote on last edited by
                            #13

                            @mrjj
                            ooh its very useful ! ty i write down this ;)

                            mrjjM 1 Reply Last reply
                            0
                            • martial123M martial123

                              @mrjj
                              ooh its very useful ! ty i write down this ;)

                              mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @martial123
                              A note on lambs.
                              If you want to access anything from the class where
                              you connect in, you have to capture the data.
                              like

                               connect(ui->pushButton, &QPushButton::clicked, this, [this](){
                                  this->ui->widget->xxxx
                                  });
                              

                              note the [this]
                              here we capture "this" . any variable can be captured.

                              martial123M 1 Reply Last reply
                              0
                              • mrjjM mrjj

                                @martial123
                                A note on lambs.
                                If you want to access anything from the class where
                                you connect in, you have to capture the data.
                                like

                                 connect(ui->pushButton, &QPushButton::clicked, this, [this](){
                                    this->ui->widget->xxxx
                                    });
                                

                                note the [this]
                                here we capture "this" . any variable can be captured.

                                martial123M Offline
                                martial123M Offline
                                martial123
                                wrote on last edited by
                                #15

                                @mrjj
                                Hum, i don't understand all , but i write down if in the future i need it ..

                                mrjjM 1 Reply Last reply
                                0
                                • martial123M martial123

                                  @mrjj
                                  Hum, i don't understand all , but i write down if in the future i need it ..

                                  mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @martial123
                                  well before using them, read a bit about it.
                                  It is not super complicated and can save you some slots.
                                  The capture part sounds odd, but basically we just list
                                  [var1, var2,var3 ]
                                  to have them avialable inside the lambda.
                                  It is a free-floating function so it's not part of the class like a slot is.

                                  1 Reply Last reply
                                  2

                                  • Login

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