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. Var in Lambda pass out ?
Forum Update on Monday, May 27th 2025

Var in Lambda pass out ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 407 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

    error: assignment of read-only variable ‘bv’ : bv = v;

    void MainWindow::on_action_brightness_triggered()
    {
        int bv = 0;
        QDialog *dialog = new QDialog;
        dialog->setWindowTitle("Brightness");
        dialog->setFixedSize(400,200);
        QSlider *slider = new QSlider(Qt::Horizontal);
        slider->setRange(-50, 50);
        slider->setTickPosition(QSlider::TicksBelow);
        connect(slider, &QSlider::valueChanged, [=](int v){
            bv = v;
            imageWidget->adjustBrightness(v, false);
        });
        QVBoxLayout *vbox = new QVBoxLayout;
        vbox->addWidget(slider);
        QPushButton *pushButton_confirm = new QPushButton("Confirm");
        QPushButton *pushButton_cancel = new QPushButton("Cancel");
        QHBoxLayout *hbox = new QHBoxLayout;
        hbox->addStretch();
        hbox->addWidget(pushButton_confirm);
        hbox->addWidget(pushButton_cancel);
        hbox->addStretch();
        vbox->addLayout(hbox);
        dialog->setLayout(vbox);
        connect(pushButton_confirm, SIGNAL(clicked()), dialog, SLOT(accept()));
        connect(pushButton_cancel, SIGNAL(clicked()), dialog, SLOT(reject()));
        if(dialog->exec() == QDialog::Accepted){
            imageWidget->adjustBrightness(bv, true);
        }
    }
    

    https://github.com/sonichy

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      bv goes out of scope when on_action_brightness_triggered() method ends (}). Your lambda is called afterwards, when the variable does not exist anymore.

      Make bv a class member, not local variable and it won't show an error.

      (Z(:^

      JKSHJ 1 Reply Last reply
      2
      • sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        My net friend show me the simplest way : change lambda [=] to [&].
        It works, but I do not know why.

        https://github.com/sonichy

        1 Reply Last reply
        0
        • sierdzioS sierdzio

          bv goes out of scope when on_action_brightness_triggered() method ends (}). Your lambda is called afterwards, when the variable does not exist anymore.

          Make bv a class member, not local variable and it won't show an error.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @sierdzio said in Var in Lambda pass out ?:

          bv goes out of scope when on_action_brightness_triggered() method ends (}). Your lambda is called afterwards, when the variable does not exist anymore.

          In this case, dialog->exec() blocks and runs a new event loop so bv technically remains in scope until the dialog and the connection gets destroyed.

          It's a fragile design, however.

          @sonichy said in Var in Lambda pass out ?:

          My net friend show me the simplest way : change lambda [=] to [&].
          It works, but I do not know why.

          • [=] gives your lambda a copy of bv.
          • [&] gives your lambda a reference to bv.

          See the answer of https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          3

          • Login

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