Qt5 - C++11:'This' 无法在此上下文中隐式捕获
Qt5 - C++11: 'This' cannot be implicitly captured in this context
我正在尝试将 qDebug()
语句的结果传递到 QTextEdit
但没有成功,因为我遇到了 'This' cannot be implicitly captured in this context
的编译器错误,而且我从未遇到过此错误之前。
然后在执行 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
文件。
感谢您阐明这个问题,并指出解决这个问题的正确方向。
你的 lambda 没有捕捉到它(正如错误消息告诉你的那样)但你正在尝试访问它(ui->textEditLidar->...)
您的 lambda 不捕获 ui
或 this
,因此无法调用 ui->textEditLidar->setText
。
改变这个
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
});
至此
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
});
或这个
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
});
我正在尝试将 qDebug()
语句的结果传递到 QTextEdit
但没有成功,因为我遇到了 'This' cannot be implicitly captured in this context
的编译器错误,而且我从未遇到过此错误之前。
然后在执行 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
文件。
感谢您阐明这个问题,并指出解决这个问题的正确方向。
你的 lambda 没有捕捉到它(正如错误消息告诉你的那样)但你正在尝试访问它(ui->textEditLidar->...)
您的 lambda 不捕获 ui
或 this
,因此无法调用 ui->textEditLidar->setText
。
改变这个
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
});
至此
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
});
或这个
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
});