简体   繁体   中英

Qt5 - C++11: 'This' cannot be implicitly captured in this context

I am trying to pass the result of a qDebug() statement into a QTextEdit but without success because I am getting a compiler error of 'This' cannot be implicitly captured in this context and I have never got this error before.

Then output is coming after executing a QProcess and would like to show it on the QTextEdit I have below:

线编辑

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    startLidar();
    ui->textEditLidar->setText("[STATUS] NO PROCESS STARTED: ");
    ui->textEditGUI->setText("[STATUS] NO PROCESS STARTED: ");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::startLidar()
{
    // Execution of the QProcess to make sure Lidar App Launcher opens:
    this->executeROSLidarApp = new QProcess(this);
    this->executeROSLidarApp->setProcessChannelMode(QProcess::MergedChannels);
    connect(this->executeROSLidarApp, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
            [script = this->executeROSLidarApp](int exitCode, QProcess::ExitStatus exitStatus){
            qDebug() << "[EXEC] FINISHED: " << exitCode << exitStatus;
            if(script->bytesAvailable() > 0) qDebug() << "[EXEC] buffered DATA:" << script->readAll();

            ui->textEditLidar->setText("would like to see the output of the exitCode and exitStatus"); // <-- error here

    });
    connect(this->executeROSLidarApp, &QProcess::errorOccurred, [script = this->executeROSLidarApp](QProcess::ProcessError error) {
            qDebug() << "[EXEC] error on execution: " << error << script->errorString();

            ui->textEditLidar->setText("would like to see the output of the error"); // <-- error here
    });
}

I think this is connected to the different C++ edition I am using. I have been using C++11 and it seems to be connected to a possible lambda function compilation error. I have been trying to research this error and came across this source , this additional source and everything seems to be leading to a mismatch between the different editions. In case needed I could also include my .pro file.

Thanks for shedding light on this issue and point to the right direction for solving this problem.

Your lambda does not capture this (as the error message tells you) but you're trying to access it (ui->textEditLidar->...)

Your lambda does not capture ui or this , so there's no way to call ui->textEditLidar->setText .

Change this

connect(this->executeROSLidarApp, &QProcess::errorOccurred, [script = this->executeROSLidarApp](QProcess::ProcessError error) {
        qDebug() << "[EXEC] error on execution: " << error << script->errorString();

        ui->textEditLidar->setText("would like to see the output of the error"); // <-- error here
});

to this

connect(this->executeROSLidarApp, &QProcess::errorOccurred, [this, script = this->executeROSLidarApp](QProcess::ProcessError error) {
        qDebug() << "[EXEC] error on execution: " << error << script->errorString();

        ui->textEditLidar->setText("would like to see the output of the error"); // <-- error here
});

or this

connect(this->executeROSLidarApp, &QProcess::errorOccurred, [ui, script = this->executeROSLidarApp](QProcess::ProcessError error) {
        qDebug() << "[EXEC] error on execution: " << error << script->errorString();

        ui->textEditLidar->setText("would like to see the output of the error"); // <-- error here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM