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 that uses signal argument to connect to a slot
Forum Updated to NodeBB v4.3 + New Features

Lambda that uses signal argument to connect to a slot

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 19.1k Views 4 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.
  • JonBJ JonB

    @ofmrew
    With the warning that I don't do C++ now and when I did I didn't use lambdas... but I can't help sticking my neck out :)

    A lambda is essentially a call to an anonymous function, with nasty syntax. If you wrote your slot as a function, you would pass a parameter for the string (received from the signal) to it, wouldn't you? Like:

    void slot(const QString& st)
    {
        this->ui->statusBar->showMessage(st, 0);
    }
    

    So when you want to do that in lambda, @sierdzio is telling you that the formal parameter belongs inside the (). Sooo... I believe you want:

        connect(ui->canvas, &MyCanvas::mouseLoc,
                   [this](const QString& st){this->ui->statusBar->showMessage(st, 0);});
    

    We could lay it out so we can see what it is going on:

        connect(ui->canvas, &MyCanvas::mouseLoc,
                   [this](const QString& st)
                   {
                       this->ui->statusBar->showMessage(st, 0);
                   }
               );
    

    See how it's really the same syntax as when you do it with an explicit function?

    O Offline
    O Offline
    ofmrew
    wrote on last edited by
    #9

    @JonB Still no dice. I tried:

    connect(ui->canvas, &MyCanvas::mouseLoc, [=](MyCanvas::mouseLoc(QString st)){ui->statusBar->showMessage(st, 0)};));
    

    No type named mouseLoc.

    sierdzioS 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #10

      Where is mouseLoc used ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      O 1 Reply Last reply
      0
      • SGaistS SGaist

        Where is mouseLoc used ?

        O Offline
        O Offline
        ofmrew
        wrote on last edited by
        #11

        @SGaist ```
        /void MyCanvas::mouseMoveEvent(QMouseEvent *e)
        {
        qDebug() << e << this->rect();
        QPoint mouseLocation = e->pos();
        qDebug() << mouseLocation;
        QPoint worldPosition = currentMatrix.inverted().map(mouseLocation);
        QString x = QString::number(worldPosition.rx());
        QString y = QString::number(worldPosition.ry());
        QString pt = x.append(":");
        mouseLocString = pt.append(y);
        emit mouseLoc(mouseLocString);
        if (currentAction == 2){
        corner2Ptw = e->pos();
        this->update();
        }
        }

        This is the code workaround that declares mouseLocString.  I would like to eliminate that step. I am, however, beginning to believe that it is a futile effort. Like I said above, I should just declare the signal with both parameters and connect it to the StatusBar slot. But I wanted to see how far I can push the boundaries of lambda. I find them very useful. Sorry if I have wasted your time.
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #12

          Eliminate what step ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mpergand
            wrote on last edited by
            #13

            @ofmrew said in Lambda that uses signal argument to connect to a slot:

            connect(ui->canvas, &MyCanvas::mouseLoc, [=](MyCanvas::mouseLoc(QString st)){ui->statusBar->showMessage(st, 0)};));

            connect(ui->canvas, &MyCanvas::mouseLoc, [](QPoint p)
            {
            ui->statusBar->showMessage(QString("%1:%2").arg(p.x()).arg(p.y()));
            });

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #14

              You can't avoid the connection step. Otherwise, how would Qt know what to call ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • O ofmrew

                @JonB Still no dice. I tried:

                connect(ui->canvas, &MyCanvas::mouseLoc, [=](MyCanvas::mouseLoc(QString st)){ui->statusBar->showMessage(st, 0)};));
                

                No type named mouseLoc.

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #15

                @ofmrew said in Lambda that uses signal argument to connect to a slot:

                @JonB Still no dice. I tried:

                connect(ui->canvas, &MyCanvas::mouseLoc, [=](MyCanvas::mouseLoc(QString st)){ui->statusBar->showMessage(st, 0)};));
                

                No type named mouseLoc.

                But @JonB wrote you exactly what code to use and you tried something else. Try what he proposed.

                (Z(:^

                O 1 Reply Last reply
                3
                • sierdzioS sierdzio

                  @ofmrew said in Lambda that uses signal argument to connect to a slot:

                  @JonB Still no dice. I tried:

                  connect(ui->canvas, &MyCanvas::mouseLoc, [=](MyCanvas::mouseLoc(QString st)){ui->statusBar->showMessage(st, 0)};));
                  

                  No type named mouseLoc.

                  But @JonB wrote you exactly what code to use and you tried something else. Try what he proposed.

                  O Offline
                  O Offline
                  ofmrew
                  wrote on last edited by
                  #16

                  @sierdzio I tried:

                      connect(ui->canvas, &MyCanvas::mouseLoc,
                          [this](const QString& st)
                          {this->ui->statusBar->showMessage(st, 0);});
                  

                  And it worked. Thanks everyone, I learned something.

                  sierdzioS 1 Reply Last reply
                  0
                  • O ofmrew

                    @sierdzio I tried:

                        connect(ui->canvas, &MyCanvas::mouseLoc,
                            [this](const QString& st)
                            {this->ui->statusBar->showMessage(st, 0);});
                    

                    And it worked. Thanks everyone, I learned something.

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #17

                    @ofmrew said in Lambda that uses signal argument to connect to a slot:

                    @sierdzio I tried:

                        connect(ui->canvas, &MyCanvas::mouseLoc,
                            [this](const QString& st)
                            {this->ui->statusBar->showMessage(st, 0);});
                    

                    And it worked. Thanks everyone, I learned something.

                    Cheers :-)

                    For the future, one more recommendation:

                    connect(ui->canvas, &MyCanvas::mouseLoc,
                             this, // <- control object
                             [this](const QString& st)
                             {this->ui->statusBar->showMessage(st, 0);});
                    

                    Adding a control object allows Qt to disconnect the connection automatically when that object is deleted. It does not matter in this case (your canvas is part of UI which is part of this), but in other situations it is a good thing to remember about (and costs nothing).

                    (Z(:^

                    O 1 Reply Last reply
                    4
                    • sierdzioS sierdzio

                      @ofmrew said in Lambda that uses signal argument to connect to a slot:

                      @sierdzio I tried:

                          connect(ui->canvas, &MyCanvas::mouseLoc,
                              [this](const QString& st)
                              {this->ui->statusBar->showMessage(st, 0);});
                      

                      And it worked. Thanks everyone, I learned something.

                      Cheers :-)

                      For the future, one more recommendation:

                      connect(ui->canvas, &MyCanvas::mouseLoc,
                               this, // <- control object
                               [this](const QString& st)
                               {this->ui->statusBar->showMessage(st, 0);});
                      

                      Adding a control object allows Qt to disconnect the connection automatically when that object is deleted. It does not matter in this case (your canvas is part of UI which is part of this), but in other situations it is a good thing to remember about (and costs nothing).

                      O Offline
                      O Offline
                      ofmrew
                      wrote on last edited by
                      #18

                      @sierdzio Thanks, I am learning even more. I made that change. Any other wise tips, the would be gratefully accepted.

                      1 Reply Last reply
                      0

                      • Login

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