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. Using push_back of vector to report error in lambda expression
Forum Update on Monday, May 27th 2025

Using push_back of vector to report error in lambda expression

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 479 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.
  • M Offline
    M Offline
    monkey233
    wrote on 16 Nov 2020, 09:23 last edited by
    #1
        std::vector<int> ii;
         connect(but1, &QPushButton::clicked, this, [&]() {
    	ii.push_back(1);
    });
    

    breakdown when i push the button. qvector is the same.

    J 1 Reply Last reply 16 Nov 2020, 09:24
    0
    • M monkey233
      16 Nov 2020, 09:23
          std::vector<int> ii;
           connect(but1, &QPushButton::clicked, this, [&]() {
      	ii.push_back(1);
      });
      

      breakdown when i push the button. qvector is the same.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 16 Nov 2020, 09:24 last edited by
      #2

      @monkey233 more information please, is ii local scope or a class member ?


      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        monkey233
        wrote on 16 Nov 2020, 09:35 last edited by
        #3

        @monkey233 said in Using push_back of vector to report error in lambda expression:

        Thank you for your reply. I'm a novice.sorry I don't know what you mean.i think ii is vector<int>. These three sentences together are correct when compiling. I omitted the statement of creating button.

        J 1 Reply Last reply 16 Nov 2020, 09:42
        0
        • M monkey233
          16 Nov 2020, 09:35

          @monkey233 said in Using push_back of vector to report error in lambda expression:

          Thank you for your reply. I'm a novice.sorry I don't know what you mean.i think ii is vector<int>. These three sentences together are correct when compiling. I omitted the statement of creating button.

          J Offline
          J Offline
          JonB
          wrote on 16 Nov 2020, 09:42 last edited by
          #4

          @monkey233
          So if it's exactly as you show, ii is a local variable to the method you are in. That means it goes out of scope (i.e. gets destroyed) when you exit that function. Meanwhile, you have a lambda-slot attached to the signal which still references that variable, which now does not exist. Kaputt....

          Presumably, if you want that to do anything permanent/meaningful, std::vector<int> ii needs to be a member variable in your class, so that it persists.

          1 Reply Last reply
          2
          • M Offline
            M Offline
            monkey233
            wrote on 16 Nov 2020, 10:45 last edited by
            #5

            thank you,i will try

            1 Reply Last reply
            0

            1/5

            16 Nov 2020, 09:23

            • Login

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