父进程将字符串写入"Message\n"子进程stdin.但是子进程没有收到它.代码中的问题在哪里?
Qt 4.7.3
父流程代码:
// class TestParent : public QMainWindow
void TestParent::createChildProcess()
{
childProcess = new QProcess( this );
connect( childProcess, SIGNAL( started() ),
this, SLOT( childProcessStarted() ) );
connect( childProcess, SIGNAL( bytesWritten( qint64 ) ),
this, SLOT( bytesWritten( qint64 ) ) );
childProcess->start( "TestChild.exe", QProcess::ReadWrite );
}
void TestParent::writeToChildProcessOutput()
{
qint64 bytesWritten = childProcess->write( "Message\n" );
qDebug() << "ret: " << bytesWritten << " bytes written";
}
void TestParent::bytesWritten()
{
qDebug() << "slot: " << bytesWritten << " bytes written";
}
Run Code Online (Sandbox Code Playgroud)
子进程代码:
// class TestChild : public QMainWindow
void TestChild::TestChild()
// QFile TestChild::input;
connect( &input, SIGNAL( readyRead() ),
this, SLOT( readInput() ) );
input.open( 0, QIODevice::ReadOnly ); // stdin
}
void TestChild::readInput()
{
QString line;
line.append( '(' );
line.append( QString::number( input.bytesAvailable() ) )
line.append( ')' );
line.append( input.readAll() );
list.append( line ); // add line to QListView
}
Run Code Online (Sandbox Code Playgroud)
对于stdin/stdout事件,无法在Qt事件循环中进行可移植连接.以下适用于非Windows平台:
QSocketNotifier *n1 = new QSocketNotifier(0, QSocketNotifier::Read, this);
connect(n1, SIGNAL(activated(int)), this, SLOT(readInputChannel()));
QSocketNotifier *n2 = new QSocketNotifier(0, QSocketNotifier::Exception, this);
connect(n2, SIGNAL(activated(int)), this, SLOT(brokenInputChannel()));
Run Code Online (Sandbox Code Playgroud)
"0"是文件描述符(stdin).
我将使用上面的内容,然后通过阻塞线程在Windows上模拟类似的东西,该线程从stdin读取并生成一个信号:
class StdinThread : public QThread
{
Q_OBJECT
signals:
void incomingData(QByteArray data);
public:
void run(void)
{
char buf[1024];
while (1)
{
int sz = fread(buf, 1, 1024, stdin);
if (sz == 0)
return;
emit incomingData(QByteArray(buf, sz));
}
}
};
Run Code Online (Sandbox Code Playgroud)
然后,在子进程中:
StdinThread *t = new StdinThread(this);
connect(t, SIGNAL(incomingData(QByteArray)), this, SLOT(processInputData(QByteArray)));
connect(t, SIGNAL(finished()), this, SLOT(brokenInputChannel()));
t->run();
Run Code Online (Sandbox Code Playgroud)
我相信您需要做的是从实际的 QProcess 对象中读取(在本例中为 childProcess,如果我理解正确的话)。
注意QProcess实际上是QIODevice的子类,还要特别注意以下函数:
QProcess::setReadChannel()
QProcess::readAllStandardOutput()
Run Code Online (Sandbox Code Playgroud)
您应该能够使用 QProcess 对象的 readyRead() 信号,而不是创建 QFile 来尝试读取标准输入。这不起作用的原因是您的 QFile 与您的 MainWindow 关联,而不是与您的子进程关联。