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
Forum Updated to NodeBB v4.3 + New Features

lambda capture syntax

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 823 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.
  • T Offline
    T Offline
    TheCipo76
    wrote on 27 Aug 2021, 06:12 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 E 2 Replies Last reply 27 Aug 2021, 06:18
    0
    • T TheCipo76
      27 Aug 2021, 06:12

      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 Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 27 Aug 2021, 06:18 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.

      T 1 Reply Last reply 27 Aug 2021, 06:24
      0
      • T TheCipo76
        27 Aug 2021, 06:12

        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?

        E Offline
        E Offline
        eyllanesc
        wrote on 27 Aug 2021, 06:22 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 J.Hilk
          27 Aug 2021, 06:18

          @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
          T Offline
          T Offline
          TheCipo76
          wrote on 27 Aug 2021, 06:24 last edited by
          #4

          @J-Hilk QCheckBox *arraySco[10];

          J 1 Reply Last reply 27 Aug 2021, 06:26
          0
          • T TheCipo76
            27 Aug 2021, 06:24

            @J-Hilk QCheckBox *arraySco[10];

            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 27 Aug 2021, 06:26 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.

            T 1 Reply Last reply 27 Aug 2021, 06:32
            2
            • J J.Hilk
              27 Aug 2021, 06:26

              @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;});
              
              T Offline
              T Offline
              TheCipo76
              wrote on 27 Aug 2021, 06:32 last edited by
              #6

              @J-Hilk Great! Thank you very much.

              J 1 Reply Last reply 27 Aug 2021, 06:42
              1
              • T TheCipo76
                27 Aug 2021, 06:32

                @J-Hilk Great! Thank you very much.

                J Offline
                J Offline
                JonB
                wrote on 27 Aug 2021, 06:42 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

                1/7

                27 Aug 2021, 06:12

                • Login

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