Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. help to find "connect" error
QtWS25 Last Chance

help to find "connect" error

Scheduled Pinned Locked Moved Unsolved C++ Gurus
11 Posts 4 Posters 814 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on 4 Feb 2024, 02:09 last edited by
    #1
    This post is deleted!
    C A 2 Replies Last reply 4 Feb 2024, 07:27
    0
    • A Anonymous_Banned275
      4 Feb 2024, 02:09

      This post is deleted!

      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 4 Feb 2024, 07:27 last edited by
      #2

      Do you really think spamming us with the same question over and over again will help you in any way? Maybe starting with basic c++ stuff would be a much better option.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • A Anonymous_Banned275
        4 Feb 2024, 02:09

        This post is deleted!

        A Offline
        A Offline
        Anonymous_Banned275
        wrote on 5 Feb 2024, 01:15 last edited by
        #3
        This post is deleted!
        C J J 3 Replies Last reply 5 Feb 2024, 05:15
        0
        • A Anonymous_Banned275
          5 Feb 2024, 01:15

          This post is deleted!

          C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 5 Feb 2024, 05:15 last edited by
          #4

          As long as you won't format your code correctly noone can read your code.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • A Anonymous_Banned275
            5 Feb 2024, 01:15

            This post is deleted!

            J Offline
            J Offline
            JonB
            wrote on 5 Feb 2024, 09:29 last edited by JonB 2 May 2024, 09:30
            #5

            @AnneRanch said in help to find "connect" error:

            connect(subMenu[index], &QMenu::triggered, this ,= { this->processMenu(index,index_sub) ;});

            It's hard to know what you are saying here, what you are surprised at or what you expect which you claim does not happen. Let's insert an extra line above the connect() and change the lambda slot slightly so it reads:

            qDebug() << index << index_sub;
            connect(subMenu[index], &QMenu::triggered, this, [=]() { qDebug() << index << index_sub; } );
            
            • When these two lines of code execute, i.e. the connect() statement is executed, you will see the values for index & index_sub at that instant. I don't know what they are, but let's say 1 & 3.

            • When whatever QMenu item was at subMenu[index] at the time this was executed is later triggered, you will see the same values for index & index_sub as they were when the connect() line was previously executed. In the above case they would be 1 and 3 again.

            Of course, if you are executing the connect() inside a loop(s) where index and/or index_sub are changing you will end up with more than one lambda slot attached. You will (potentially) have more than one slot attached to a given QMenu item and/or slot(s) attached to more than one QMenu item.

            • Each time index changes you will be attaching to a different subMenu[index] item.
            • Each time index_sub changes you will be attaching to the same subMenu[index] item but with a different value for the qDebug() << index_sub in the lambda slot.
            1 Reply Last reply
            0
            • A Anonymous_Banned275
              5 Feb 2024, 01:15

              This post is deleted!

              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 5 Feb 2024, 09:42 last edited by J.Hilk 2 May 2024, 09:43
              #6

              @AnneRanch said in help to find "connect" error:

              both indexes - index and index-sub are STATIC, they are NOT created when "click on submenu" is activated.

              Well I'm not surprised you're lambda does not snapshot shot index or index_sub during the moment of the connect call but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted.

              this will snapshot index and index_sub:

              connect(subMenu[index], &QMenu::triggered, this , [this, i = index, i_s = index_sub]()->void{ this->processMenu(i,i_s) ;});
              

              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.

              J 1 Reply Last reply 5 Feb 2024, 10:08
              0
              • J J.Hilk
                5 Feb 2024, 09:42

                @AnneRanch said in help to find "connect" error:

                both indexes - index and index-sub are STATIC, they are NOT created when "click on submenu" is activated.

                Well I'm not surprised you're lambda does not snapshot shot index or index_sub during the moment of the connect call but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted.

                this will snapshot index and index_sub:

                connect(subMenu[index], &QMenu::triggered, this , [this, i = index, i_s = index_sub]()->void{ this->processMenu(i,i_s) ;});
                
                J Offline
                J Offline
                JonB
                wrote on 5 Feb 2024, 10:08 last edited by JonB 2 May 2024, 10:11
                #7

                @J-Hilk said in help to find "connect" error:

                Well I'm not surprised you're lambda does not snapshot shot index or index_sub during the moment of the connect call but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted.

                I do not understand why you have written this. The OP's lambda, and my post, use [=] as the capture. That will "snapshot" (as you call it) the values at the instant the connect() statement is executed, as I wrote. So why are you saying

                but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted

                I do not see your [this, i = index, i_s = index_sub] will behave any differently from [=] in this case? Do you really want me to go try, are you saying I have misunderstood C++ lambdas/``[=]` ??

                which it will not (as & was not used)?

                WAIT
                Are you saying index and index_sub are class member variables, not local ones wherever the code is?? I thought they were locals, I think they are, I think the OP is sjowing us some function where it has

                // results
                int submenu_index = -1;
                int mainmenu_index= -1;
                

                at the top, but it's hard to tell....

                J 1 Reply Last reply 5 Feb 2024, 10:18
                0
                • J JonB
                  5 Feb 2024, 10:08

                  @J-Hilk said in help to find "connect" error:

                  Well I'm not surprised you're lambda does not snapshot shot index or index_sub during the moment of the connect call but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted.

                  I do not understand why you have written this. The OP's lambda, and my post, use [=] as the capture. That will "snapshot" (as you call it) the values at the instant the connect() statement is executed, as I wrote. So why are you saying

                  but it will capture the reference to the member variables and look them up at the moment the triggered signal is emitted

                  I do not see your [this, i = index, i_s = index_sub] will behave any differently from [=] in this case? Do you really want me to go try, are you saying I have misunderstood C++ lambdas/``[=]` ??

                  which it will not (as & was not used)?

                  WAIT
                  Are you saying index and index_sub are class member variables, not local ones wherever the code is?? I thought they were locals, I think they are, I think the OP is sjowing us some function where it has

                  // results
                  int submenu_index = -1;
                  int mainmenu_index= -1;
                  

                  at the top, but it's hard to tell....

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 5 Feb 2024, 10:18 last edited by
                  #8

                  @JonB

                      QApplication app(argc,argv);
                  
                      static int index{0}, index_sub{0};
                  
                      auto lambda1 = [=]()->void{qDebug() << index << index_sub;};
                      auto lambda2 = [=, i = index, i_s = index_sub]()->void{qDebug() << i << i_s;};
                  
                      lambda1();
                      lambda2();
                  
                      index = 100;
                      index_sub = 100;
                  
                      lambda1();
                      lambda2();
                  

                  ->

                  0 0
                  0 0
                  100 100
                  0 0
                  

                  Are you saying index and index_sub are class member variables, not local ones wherever the code is??

                  that's what the 2nd post is saying, at least what I understand it's saying.


                  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.

                  J 1 Reply Last reply 5 Feb 2024, 11:47
                  0
                  • J J.Hilk
                    5 Feb 2024, 10:18

                    @JonB

                        QApplication app(argc,argv);
                    
                        static int index{0}, index_sub{0};
                    
                        auto lambda1 = [=]()->void{qDebug() << index << index_sub;};
                        auto lambda2 = [=, i = index, i_s = index_sub]()->void{qDebug() << i << i_s;};
                    
                        lambda1();
                        lambda2();
                    
                        index = 100;
                        index_sub = 100;
                    
                        lambda1();
                        lambda2();
                    

                    ->

                    0 0
                    0 0
                    100 100
                    0 0
                    

                    Are you saying index and index_sub are class member variables, not local ones wherever the code is??

                    that's what the 2nd post is saying, at least what I understand it's saying.

                    J Offline
                    J Offline
                    JonB
                    wrote on 5 Feb 2024, 11:47 last edited by JonB 2 May 2024, 11:57
                    #9

                    @J-Hilk
                    Nope! Please read, you have an "edge" case of some kind in your code.

                    To do your static int index{0}, index_sub{0}; I had to change my default C++ compilation from c++11 to c++14. There may be a clue here (actually I'm not sure this is relevant). With c++11 and your code I get

                    /home/jon/QtTests/lambdacopy/main.cpp:11: warning: lambda capture initializers only available with ‘-std=c++14’ or ‘-std=gnu++14’
                    ../lambdacopy/main.cpp: In function ‘int main(int, char**)’:
                    ../lambdacopy/main.cpp:11:24: warning: lambda capture initializers only available with ‘-std=c++14’ or ‘-std=gnu++14’
                       11 |     auto lambda2 = [=, i = index, i_s = index_sub]()->void{qDebug() << i << i_s;};
                          |                        ^
                    

                    Please change your code to remove the static, so it reads

                    /*static*/ int index{0}, index_sub{0};
                    

                    Now the output reads

                    0 0
                    0 0
                    0 0
                    0 0
                    

                    which is exactly what I expected.

                    The behaviour of [=] seems to be different on local variables which are static? But we have no evidence those would be static in OP's code, and I would never have tried with static.....

                    EDIT
                    Ohhhhh, now I see the OP's

                    both indexes - index and index-sub are STATIC, they are NOT created when "click on subnenu" is activated.

                    @AnneRanch
                    Now that I see this. (You had never posted the declarations of these variables, and no static keyword appears in any of your code, which is why we really need complete examples to help.) For your purposes you must NOT have static index, index_sub. This is why they are not delivering the values they had at the time of the connect() but rather the values they have from the last iteration. Or follow @J.Hilk's

                    [this, i = index, i_s = index_sub]()->void{ this->processMenu(i,i_s) ; }
                    

                    to copy their current values into i & i_s (or whatever) so that the lambda gets a copy of their current value.

                    J 1 Reply Last reply 5 Feb 2024, 12:02
                    0
                    • J JonB
                      5 Feb 2024, 11:47

                      @J-Hilk
                      Nope! Please read, you have an "edge" case of some kind in your code.

                      To do your static int index{0}, index_sub{0}; I had to change my default C++ compilation from c++11 to c++14. There may be a clue here (actually I'm not sure this is relevant). With c++11 and your code I get

                      /home/jon/QtTests/lambdacopy/main.cpp:11: warning: lambda capture initializers only available with ‘-std=c++14’ or ‘-std=gnu++14’
                      ../lambdacopy/main.cpp: In function ‘int main(int, char**)’:
                      ../lambdacopy/main.cpp:11:24: warning: lambda capture initializers only available with ‘-std=c++14’ or ‘-std=gnu++14’
                         11 |     auto lambda2 = [=, i = index, i_s = index_sub]()->void{qDebug() << i << i_s;};
                            |                        ^
                      

                      Please change your code to remove the static, so it reads

                      /*static*/ int index{0}, index_sub{0};
                      

                      Now the output reads

                      0 0
                      0 0
                      0 0
                      0 0
                      

                      which is exactly what I expected.

                      The behaviour of [=] seems to be different on local variables which are static? But we have no evidence those would be static in OP's code, and I would never have tried with static.....

                      EDIT
                      Ohhhhh, now I see the OP's

                      both indexes - index and index-sub are STATIC, they are NOT created when "click on subnenu" is activated.

                      @AnneRanch
                      Now that I see this. (You had never posted the declarations of these variables, and no static keyword appears in any of your code, which is why we really need complete examples to help.) For your purposes you must NOT have static index, index_sub. This is why they are not delivering the values they had at the time of the connect() but rather the values they have from the last iteration. Or follow @J.Hilk's

                      [this, i = index, i_s = index_sub]()->void{ this->processMenu(i,i_s) ; }
                      

                      to copy their current values into i & i_s (or whatever) so that the lambda gets a copy of their current value.

                      J Offline
                      J Offline
                      J.Hilk
                      Moderators
                      wrote on 5 Feb 2024, 12:02 last edited by
                      #10

                      @JonB what exactly is your point of content with my example/solution?

                      With c++11 and your code I get

                      of course, capture initalizeres [i = index] are a c++17 feature independently from index being static or not

                      Please change your code to remove the static, so it reads
                      Now the output reads

                      Of course, I would expect nothing else. I'm pretty sure one of either the capture initialisers or the capture by copy of the local variables are skipped by the compiler.

                      The behaviour of [=] seems to be different on local variables which are static? But we have no evidence those would be static in OP's code, and I would never have tried with static.....

                      well in this case, static and/or member variables. I was too lazy to write a whole class for this example. And like I quoted, her 2nd comment clearly states staticmember variables of index and index_sub


                      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.

                      J 1 Reply Last reply 5 Feb 2024, 12:13
                      0
                      • J J.Hilk
                        5 Feb 2024, 12:02

                        @JonB what exactly is your point of content with my example/solution?

                        With c++11 and your code I get

                        of course, capture initalizeres [i = index] are a c++17 feature independently from index being static or not

                        Please change your code to remove the static, so it reads
                        Now the output reads

                        Of course, I would expect nothing else. I'm pretty sure one of either the capture initialisers or the capture by copy of the local variables are skipped by the compiler.

                        The behaviour of [=] seems to be different on local variables which are static? But we have no evidence those would be static in OP's code, and I would never have tried with static.....

                        well in this case, static and/or member variables. I was too lazy to write a whole class for this example. And like I quoted, her 2nd comment clearly states staticmember variables of index and index_sub

                        J Offline
                        J Offline
                        JonB
                        wrote on 5 Feb 2024, 12:13 last edited by
                        #11

                        @J-Hilk said in help to find "connect" error:

                        And like I quoted, her 2nd comment clearly states static member variables of index and index_sub

                        Evidently easier to spot that by some (you) than by others (me)! There is a lot to wade through (unformatted), and it always helps to show the relevant code...! :)

                        Anyway, hopefully the OP has a couple of alternative ways to modify the code now to get the behaviour desired.

                        1 Reply Last reply
                        1

                        3/11

                        5 Feb 2024, 01:15

                        topic:navigator.unread, 8
                        • Login

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