【问题标题】:Attempting to read stdout from spawned QProcess试图从生成的 QProcess 中读取标准输出
【发布时间】:2015-06-19 07:08:47
【问题描述】:

我编写了一个 Qt 应用程序来为后端 Linux 控制台应用程序创建 UI。现在,我希望控制台输出和标准错误显示在我的应用程序的窗口中。

我已经尝试了以下代码:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);

}

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

void MainWindow::on_pushButton_clicked()
{
    QProcess *console_backend = new QProcess(this);
    QString file = "/path/to/console_executable";
    console_backend->start(file);

    console_backend->setReadChannel(QProcess::StandardOutput);

    QObject::connect(console_backend, SIGNAL(console_backend
                     ->readyReadStandardOutput()),
                     this, SLOT(this->receiveConsoleBackendOutput()));

}

void MainWindow::receiveConsoleBackendOutput()
{
    //..celebrate !
}

当我执行应用程序时,我得到的错误是:

Object::connect: No such signal
QProcess::console_backend>readyReadStandardOutput() in
../projekt_directory/mainwindow.cpp:239 
Object::connect:  (receiver name: 'MainWindow')

我对信号槽的理解有误吗?为什么是“没有这样的信号”? 整个方法是错误的吗?

【问题讨论】:

  • 你会的。 connect(console_backend, SIGNAL(readyReadStandardOutput()), this, SLOT(receiveConsoleBackendOutput()));
  • @Amartel 哦该死。那是真正的代码复制粘贴。它应该是 console_backend 代替 real_code。您的建议可以消除错误。
  • SIGNALSLOT 是宏,它们将方法的名称转换为字符串,因此您需要删除所有 this-> 等调用。只是方法的签名。阅读 Qt 的手册了解更多示例。

标签: qt signals-slots qprocess


【解决方案1】:

问题在声明中

QObject::connect(console_backend, SIGNAL(console_backend
                 ->readyReadStandardOutput()),
                 this, SLOT(this->receiveConsoleBackendOutput()));

应该是

QObject::connect(console_backend, SIGNAL(readyReadStandardOutput()),
                 this, SLOT(receiveConsoleBackendOutput()));

【讨论】:

    猜你喜欢
    • 2014-07-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    相关资源
    最近更新 更多