【发布时间】:2011-09-17 21:37:59
【问题描述】:
父进程将字符串"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";
}
子进程代码:
// 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
}
【问题讨论】:
-
TestChild 是 QProcess 吗?并且输入是 QIODevice,而不是子类?
-
@Chris:不,TestChild - 子进程的主窗口对象。在父进程中,我们使用
childProcess对象创建子进程。我的错误:input是 QFile,谢谢。