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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on 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

    jsulmJ 1 Reply Last reply
    0
    • sonichyS sonichy

      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); });
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #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

      sonichyS 1 Reply Last reply
      6
      • jsulmJ jsulm

        @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)

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on 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

        jsulmJ 1 Reply Last reply
        0
        • sonichyS sonichy

          @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));
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #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

          sonichyS 1 Reply Last reply
          3
          • jsulmJ jsulm

            @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));
            
            sonichyS Offline
            sonichyS Offline
            sonichy
            wrote on 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

            jsulmJ 1 Reply Last reply
            0
            • sonichyS sonichy

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

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

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by VRonin
              #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
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by VRonin
                #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

                • Login

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