【问题标题】:Qt5 - C++11: 'This' cannot be implicitly captured in this contextQt5 - C++11:“This”不能在这个上下文中被隐式捕获
【发布时间】:2020-09-24 23:21:53
【问题描述】:

我正在尝试将 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 函数编译错误有关。 我一直在尝试研究这个错误,并遇到了this sourcethis additional 源,一切似乎都导致了不同版本之间的不匹配。如果需要,我还可以包含我的 .pro 文件。

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

【问题讨论】:

    标签: c++ qt c++11 qt5 qtextedit


    【解决方案1】:

    您的 lambda 没有捕获 uithis,因此无法调用 ui-&gt;textEditLidar-&gt;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
    });
    

    【讨论】:

    • 非常感谢您阅读这个问题。那应该怎么修呢?我以前从未遇到过这个错误?
    • @Emanuele 当然,解决方法是在 lambda 中捕获 thisui
    • 您能否在您的答案中加入一些代码,以便我更好地理解您的建议:)?
    • 噢,我抓住了你!那很简单!!非常感谢! :)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-25
      • 2012-12-25
      • 1970-01-01
      • 2012-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多