首页 > 解决方案 > QProcess 无法触发信号 readyRead/readyReadStandardOutput/readyReadStandardError

问题描述

当我运行以下命令时,为什么我永远不会得到 readyRead/readyReadStandardOutput/readyReadStandardError 信号?我在控制台中获得所有输出。我正在使用 Qt4.8 应用程序来调用 lubuntu 16.04 64bit 中的子进程。这个问题一直困扰着我很长时间。我曾经在 win7 上尝试相同的代码,它完美地工作。

主窗口头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>
namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
        Q_OBJECT

    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        QProcess* process;
    private slots:
        void on_pushButton_clicked();
        void OnRead();
    private:
        Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

主窗口源:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QProcess>
#include <QDebug>
MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    process = new QProcess(this);
    bool result = connect(process, SIGNAL(readyRead()), SLOT(OnRead()));
    qDebug() << result;
    connect(process, SIGNAL(readyReadStandardOutput()), this, SLOT(OnRead()));
    connect(process, SIGNAL(readyReadStandardError()), this, SLOT(OnRead()));
    process->setProcessChannelMode(QProcess::ForwardedChannels);
    process->start("/home/albert/test");
}

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

void MainWindow::on_pushButton_clicked()
{
}
void MainWindow::OnRead()
{
    qDebug() << "can read";
}

测试代码在这里:

#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> /* Definition of uint64_t */
#include <iostream>

int main(int argc, char* argv[])
{
    while (1) {
        std::cout << "hello world!0";
        printf("hello world!\n");
        fprintf(stderr, "hello world error!\n");
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

标签: c++qt

解决方案


根据我的评论,使用setProcessChannelMode(QProcess::ForwardedChannels)导致以下行为......

QProcess 将正在运行的进程的输出转发到主进程。子进程写入其标准输出和标准错误的任何内容都将写入主进程的标准输出和标准错误。

至于为什么这可能会在 Windows 上产生各种readyRead*信号,我只能猜测,如果父进程实际上没有任何与之关联的控制台(例如 GUI 进程),那么调用setProcessChannelMode将被忽略,让您使用默认通道模式的QProcess::SeparateChannels

关于输出中额外的双引号,这正是qDebug某些类型(例如QByteArray等)QString的作用。如果要删除引号,请尝试...

qDebug().noquote() << process->readAllStandardOutput();

推荐阅读