Qt的5和QProcess中重定向标准输出与信号/槽readyRead [英] Qt 5 and QProcess redirect stdout with signal/slot readyRead

查看:4065
本文介绍了Qt的5和QProcess中重定向标准输出与信号/槽readyRead的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题困扰着我,因为它应该工作,但遗憾的是它没有。
我试图做到的,是阅读一定的过程的标准输出,使另一个进程处理它即打印出来。

This problem is bothering me because it should work, but sadly it does not. What i try to achieve is to read the standard output of a certain process and make another process handle it i.e. print it out.

这产生输出的过程是这样的:

The process that produces output looks like this:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

int main() {
    for (int i = 0; i < 100; i++) {
        printf("yes %d\n",i);
        fflush(stdout);
        sleep(1);
    }
    return 0;
}

的过程中这样的另一个应用程序启动

The process is started in another application like this:

#include <QProcess>
...
QProcess * process = new QProcess;
SomeClass * someClass = new SomeClass(process);
connect(process,SIGNAL(readyRead()),someClass,SLOT(onReadyRead()));

process->start("../Test/Test",QStringList());
if (!process->waitForStarted(4000)) {
    qDebug() << "Process did not start.";
}
...
void SomeClass::onReadyRead() {
    qDebug() << "Reading:" << process->readAllStdOutput();
}

我预期的产出将是:

My expected output would be:

Reading: yes 0
Reading: yes 1
...
Reading: yes 99

但是我得到任何输出。
当我使用QCoreApplication我得到的所有输出,但不通过信号/槽,而是直接在控制台中。

However i get no output at all. And when i use QCoreApplication i get all the output but not through the signal/slot but directly in the console.

我不明白,因为它的工作原理在使用Qt 4.8的另一个应用程序。

I dont understand because it works in another application that uses Qt 4.8.

我的问题是,有没有人遇到同样的问题还是没有人知道如何我能得到预期的行为?

My question is, is anyone experiencing the same problem or does anyone know how i can get the expected behaviour?

推荐答案

嗯,我解决我的问题。

如果该进程始于startDetached(),它不会从接收信号的 readyRead() readyReadStandardOutput()的和的 readyReadStandardError()

If the process is started with startDetached() it will not receive the signals from readyRead(), readyReadStandardOutput() and readyReadStandardError().

所以只是开始启动它()解决了这个问题。

So just starting it with start() solved the problem.

不过,我注意到,如果我开始做while循环,并打印的的main()的将在一次,即使它\\ n结尾读到的一切。于是我开始在一个线程while循环,并同时解决了这个问题。一切都打印效果与预期。

However i noticed that if i start and do the while loop and prints in main() it will read everything at once even if it ends with \n. So i started the while loop in a thread and that problem was also solved. Everything prints as expected.

#include <QThread>

class Thread : public QThread 
{
    Q_OBJECT

public:
    explicit Thread(QObject *parent = 0) : QThread(parent) {}

protected:
    void run() {
        for (int i = 0; i < 100; i++) {
            std::cout << "yes" << i << std::endl;
            msleep(200);
        }
        exit(0);
    }
};

int main(int argc, char ** argv) {
    QCoreApplication app(argc,argv);
    Thread * t = new Thread();
    t->start();
    return app.exec();
}

TestP的main.cpp

TestP main.cpp

#include <QProcess>
#include <iostream>

class Controller : public QObject 
{
    Q_OBJECT
private:
    QProcess * process;

public:
    Controller(QObject *parent = 0) : 
        QObject(parent), process(new QProcess) {}

    void init(const QString &program) {
        connect(process,SIGNAL(readyRead()),this,SLOT(readStdOut()));
        connect(process,SIGNAL(started()),this,SLOT(onStarted()));
        connect(process,SIGNAL(finished(int)),this,SLOT(onFinished(int)));
        process->start(program);
    }

private slots:
    void readStdOut() {
        std::cout << "YES " << QString(process->readAllStandardOutput()).toUtf8().constData() << std::endl;
    }
    void onStarted(){
        std::cout << "Process started" << std::endl;
    }
    void onFinished(int) {
        std::cout << "Process finished: " << signal << std::endl;
    }
};

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    Controller c;
    c.init("../Test/Test");
    return a.exec();
}

这篇关于Qt的5和QProcess中重定向标准输出与信号/槽readyRead的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆