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. lambda capture syntax
Qt 6.11 is out! See what's new in the release blog

lambda capture syntax

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 2.2k Views 1 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.
  • TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by TheCipo76
    #1

    Hi to all,
    i have to capture array index and pass it to a slot..
    i've read some post about lambda but i can't do it..

    //arraySco[i] is a QCheckBox
    //connect(arraySco[i], SIGNAL(stateChanged()), this, CambiaMag(i)); //this is the goal
    connect(arraySco[i], SIGNAL(stateChanged()), this, [=] () {qDebug() << i;});
    

    generate several errors..
    i want capture "i" var and pass it to a SLOT (CambiaMag())
    anyone can help me please?

    J.HilkJ eyllanescE 2 Replies Last reply
    0
    • TheCipo76T TheCipo76

      Hi to all,
      i have to capture array index and pass it to a slot..
      i've read some post about lambda but i can't do it..

      //arraySco[i] is a QCheckBox
      //connect(arraySco[i], SIGNAL(stateChanged()), this, CambiaMag(i)); //this is the goal
      connect(arraySco[i], SIGNAL(stateChanged()), this, [=] () {qDebug() << i;});
      

      generate several errors..
      i want capture "i" var and pass it to a SLOT (CambiaMag())
      anyone can help me please?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @TheCipo76 said in lambda capture syntax:

      connect(arraySco[i], SIGNAL(stateChanged()), this, [=] () {qDebug() << i;});

      that won't work, you'll need to use the new function pointer based syntax.

      I would correct it for you but I'm missing crucial information.

      • pointer to the class instance that has the stateChanged signal
      • Name of the class that has the stateChanged signal

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      TheCipo76T 1 Reply Last reply
      0
      • TheCipo76T TheCipo76

        Hi to all,
        i have to capture array index and pass it to a slot..
        i've read some post about lambda but i can't do it..

        //arraySco[i] is a QCheckBox
        //connect(arraySco[i], SIGNAL(stateChanged()), this, CambiaMag(i)); //this is the goal
        connect(arraySco[i], SIGNAL(stateChanged()), this, [=] () {qDebug() << i;});
        

        generate several errors..
        i want capture "i" var and pass it to a SLOT (CambiaMag())
        anyone can help me please?

        eyllanescE Offline
        eyllanescE Offline
        eyllanesc
        wrote on last edited by
        #3

        @TheCipo76 Use new syntax connection: https://wiki.qt.io/New_Signal_Slot_Syntax

        If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

        1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @TheCipo76 said in lambda capture syntax:

          connect(arraySco[i], SIGNAL(stateChanged()), this, [=] () {qDebug() << i;});

          that won't work, you'll need to use the new function pointer based syntax.

          I would correct it for you but I'm missing crucial information.

          • pointer to the class instance that has the stateChanged signal
          • Name of the class that has the stateChanged signal
          TheCipo76T Offline
          TheCipo76T Offline
          TheCipo76
          wrote on last edited by
          #4

          @J-Hilk QCheckBox *arraySco[10];

          J.HilkJ 1 Reply Last reply
          0
          • TheCipo76T TheCipo76

            @J-Hilk QCheckBox *arraySco[10];

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #5

            @TheCipo76

            alight

            connect(arraySco[i], &QCheckBox::stateChanged, this, [=] ()->void {qDebug() << i;});
            

            in case you want to use the argument, because state changed has an int argument:

            connect(arraySco[i], &QCheckBox::stateChanged, this, [=] (int state)->void {qDebug() << i << state;});
            

            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            TheCipo76T 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @TheCipo76

              alight

              connect(arraySco[i], &QCheckBox::stateChanged, this, [=] ()->void {qDebug() << i;});
              

              in case you want to use the argument, because state changed has an int argument:

              connect(arraySco[i], &QCheckBox::stateChanged, this, [=] (int state)->void {qDebug() << i << state;});
              
              TheCipo76T Offline
              TheCipo76T Offline
              TheCipo76
              wrote on last edited by
              #6

              @J-Hilk Great! Thank you very much.

              JonBJ 1 Reply Last reply
              1
              • TheCipo76T TheCipo76

                @J-Hilk Great! Thank you very much.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @TheCipo76
                And just so you know. The capture [=] means "capture all local variables, by value" ([&] would be the same but all by reference). In this case you only need i, so you could have written [i] here, or [i, j, ..] for individual ones. Just so you know if you want to use that or see it elsewhere in examples.

                1 Reply Last reply
                4

                • Login

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