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: How to pass object in ?
Forum Updated to NodeBB v4.3 + New Features

Lambda: How to pass object in ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 1.6k 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.
  • S Offline
    S Offline
    sonichy
    wrote on 7 Feb 2019, 04:39 last edited by
    #1

    If do not use global variable, how to pass ui->pushButton to function of lambda ?

    ui->pushButton->setDown(true);
    QTimer::singleShot(100, ui->pushButton, [=]{ ui->pushButton->setDown(false); });
    

    https://github.com/sonichy

    J 1 Reply Last reply 7 Feb 2019, 05:25
    0
    • S sonichy
      7 Feb 2019, 04:39

      If do not use global variable, how to pass ui->pushButton to function of lambda ?

      ui->pushButton->setDown(true);
      QTimer::singleShot(100, ui->pushButton, [=]{ ui->pushButton->setDown(false); });
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 7 Feb 2019, 05:25 last edited by jsulm 2 Jul 2019, 05:26
      #2

      @sonichy said in Lambda: How to pass object in ?:

      QTimer::singleShot(100, ui->pushButton, [=]{ ui->pushButton->setDown(false); });

      You already capture all variables available in the same scope by value via [=]
      If you only want to catch a class member then do:

      QTimer::singleShot(100, ui->pushButton, [this]{ ui->pushButton->setDown(false); });
      

      Please read https://en.cppreference.com/w/cpp/language/lambda as your question isn't related to Qt (better ask such questions in https://forum.qt.io/category/34/c-gurus)

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply 7 Feb 2019, 06:40
      6
      • J jsulm
        7 Feb 2019, 05:25

        @sonichy said in Lambda: How to pass object in ?:

        QTimer::singleShot(100, ui->pushButton, [=]{ ui->pushButton->setDown(false); });

        You already capture all variables available in the same scope by value via [=]
        If you only want to catch a class member then do:

        QTimer::singleShot(100, ui->pushButton, [this]{ ui->pushButton->setDown(false); });
        

        Please read https://en.cppreference.com/w/cpp/language/lambda as your question isn't related to Qt (better ask such questions in https://forum.qt.io/category/34/c-gurus)

        S Offline
        S Offline
        sonichy
        wrote on 7 Feb 2019, 06:40 last edited by
        #3

        @jsulm I want to replace ui->pushButton in the { ... }, not the = in the [... ]
        Something like

        QTimer::singleShot(100, ui->pushButton, [=]{ sender()->setDown(false); });
        QTimer::singleShot(100, ui->pushButton, SLOT(setDown(false));
        

        https://github.com/sonichy

        J 1 Reply Last reply 7 Feb 2019, 06:44
        0
        • S sonichy
          7 Feb 2019, 06:40

          @jsulm I want to replace ui->pushButton in the { ... }, not the = in the [... ]
          Something like

          QTimer::singleShot(100, ui->pushButton, [=]{ sender()->setDown(false); });
          QTimer::singleShot(100, ui->pushButton, SLOT(setDown(false));
          
          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 7 Feb 2019, 06:44 last edited by jsulm 2 Jul 2019, 06:47
          #4

          @sonichy said in Lambda: How to pass object in ?:

          I want to replace ui->pushButton in the { ... }, not the = in the [... ]

          What does "replace" mean here?
          If you want to access class member inside the lambda (inside {}) you have to capture "this" in []. This is how lambdas work.
          This should work just fine, did you try?

          QTimer::singleShot(100, ui->pushButton, [this]{ ui->pushButton->setDown(false); });
          

          This can't work because setDown() is not a slot:

          QTimer::singleShot(100, ui->pushButton, SLOT(setDown(false));
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          S 1 Reply Last reply 8 Feb 2019, 04:07
          3
          • J jsulm
            7 Feb 2019, 06:44

            @sonichy said in Lambda: How to pass object in ?:

            I want to replace ui->pushButton in the { ... }, not the = in the [... ]

            What does "replace" mean here?
            If you want to access class member inside the lambda (inside {}) you have to capture "this" in []. This is how lambdas work.
            This should work just fine, did you try?

            QTimer::singleShot(100, ui->pushButton, [this]{ ui->pushButton->setDown(false); });
            

            This can't work because setDown() is not a slot:

            QTimer::singleShot(100, ui->pushButton, SLOT(setDown(false));
            
            S Offline
            S Offline
            sonichy
            wrote on 8 Feb 2019, 04:07 last edited by
            #5

            @jsulm I want to pass "ui->pushButton" through "this".

            QTimer::singleShot(100, ui->pushButton, [this]{ this->setDown(false); });

            https://github.com/sonichy

            J 1 Reply Last reply 8 Feb 2019, 05:17
            0
            • S sonichy
              8 Feb 2019, 04:07

              @jsulm I want to pass "ui->pushButton" through "this".

              QTimer::singleShot(100, ui->pushButton, [this]{ this->setDown(false); });

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 8 Feb 2019, 05:17 last edited by VRonin 2 Aug 2019, 18:13
              #6

              @sonichy "I want to pass "ui->pushButton" through "this"." - "ui" is part of "this", to access it you need "this", so you simply pass "this" to the lambda.
              Why don't you simply try what I suggested?!

              This is invalid code:

              QTimer::singleShot(100, ui->pushButton, [this](){ this->setDown(false); });
              

              setDown() is a method of a QPushButton instance. "this" is not a pointer to a QPushButton instance.
              To access member variables and methods in a lambda you need to capture "this" as I already explained. So, this is the solution:

              QTimer::singleShot(100, ui->pushButton, [this](){ ui->pushButton->setDown(false); });
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              8
              • V Offline
                V Offline
                VRonin
                wrote on 8 Feb 2019, 18:15 last edited by VRonin 2 Aug 2019, 18:15
                #7

                alternatively: QTimer::singleShot(100, ui->pushButton, std::bind(&QPushButton::setDown,ui->pushButton,false));. You don't need a lambda to just forward a call to another method

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                5

                1/7

                7 Feb 2019, 04:39

                • 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