首页 > 解决方案 > Qt5 - C++11:“This”不能在这个上下文中被隐式捕获

问题描述

我试图将qDebug()语句的结果传递给 aQTextEdit但没有成功,因为我遇到了编译器错误,'This' cannot be implicitly captured in this context而且我以前从未遇到过此错误。

然后输出在执行 a 后出现QProcess,并希望在QTextEdit下面显示它:

线编辑

#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
    });
}

我认为这与C++我使用的不同版本有关。我一直在使用C++11,它似乎与可能的 lambda 函数编译错误有关。我一直在尝试研究这个错误并遇到了这个来源这个额外的来源和一切似乎都导致了不同版本之间的不匹配。如果需要,我还可以包含我的.pro文件。

感谢您阐明此问题并指出解决此问题的正确方向。

标签: c++qtc++11qt5qtextedit

解决方案


您的 lambda 没有捕获这一点(正如错误消息告诉您的那样),但您正在尝试访问它(ui->textEditLidar->...)


推荐阅读