c++ - 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;
}
解决方案
根据我的评论,使用setProcessChannelMode(QProcess::ForwardedChannels)
导致以下行为......
QProcess 将正在运行的进程的输出转发到主进程。子进程写入其标准输出和标准错误的任何内容都将写入主进程的标准输出和标准错误。
至于为什么这可能会在 Windows 上产生各种readyRead*
信号,我只能猜测,如果父进程实际上没有任何与之关联的控制台(例如 GUI 进程),那么调用setProcessChannelMode
将被忽略,让您使用默认通道模式的QProcess::SeparateChannels
。
关于输出中额外的双引号,这正是qDebug
某些类型(例如QByteArray
等)QString
的作用。如果要删除引号,请尝试...
qDebug().noquote() << process->readAllStandardOutput();
推荐阅读
- android - 如何在 Android Studio 中使用 FAB 按钮查看新活动
- flutter - Flutter double Fab 在 docker 上的使用
- flutter - FutureBuilder 不等待异步函数加载
- javascript - 使用 javascript 为 div 类设置悬停
- reactjs - 谷歌云存储桶上的资产的Aframe资产加载呈现黑色平面而不是图像
- keras - 在 Keras LSTM 自动编码器中重构消耗的序列掩码
- node.js - 使用 Node.js 在 React 中使用 Express 的 LocalHost 到域名
- java - 将带有“+”的时间戳解析为 Java 中的 Date 或 Instant
- python - Python Selenium 无法与弹出窗口交互
- reactjs - 创建一个将在 React 中保存对象的数据结构