我现在正试着从qprocess中读和写。我做了一个小测试程序,它接受输入并在屏幕上循环显示。这是我来自Qt的代码
QString path = "./test";
tcd = new QProcess(this);
QStringList args;
args << "";
tcd->start(path,args);
if(!tcd->waitForStarted(3000))
{
stdoutput->append("<h1><font color=red>There was a problem starting the software, please try running the program again.</font></h1>");
}
tcd->write("hello\n");
tcd->write("hello\n");
tcd->write("hello\n");
tcd->write("hello\n");
//tcd->write("quit\n");
QObject::connect(tcd, SIGNAL(readyReadStandardOutput()), this, SLOT(appendTextBox()));除非我发送最后一个退出命令(该命令终止了我的测试程序),否则这是行不通的。
这是我的阅读命令:
void TCD2_GUI::appendTextBox(){
stdoutput->append("new output available: \n");
QByteArray newData = tcd->readAllStandardOutput();
stdoutput->append(QString::fromLocal8Bit(newData));
}如果我发送退出,我将立即得到程序的所有输出,包括我发送的所有内容。
我在这里做错什么了?
根据请求,下面是程序的代码:
int main(int argC[], char* argV[]){
printf("Welcome!\n");
char* input = malloc(160);
gets(input);
while(strcmp(input,"quit") != 0)
{
printf("Got input %s\n", input);
gets(input);
}
}发布于 2011-02-09 19:52:34
从医生那里:
QIODevice的某些子类(如QTcpSocket和QProcess )是异步的。这意味着I/O函数,例如写()或read()总是会立即返回,而当控制返回到事件循环时,可能会发生与设备本身的通信。QIODevice提供了一些函数,允许您强制这些操作立即执行(),同时阻塞调用线程,而无需进入事件循环。 ..。 waitForBytesWritten() -此函数在调用线程中挂起操作,直到向设备写入了一个有效数据。 ..。 从主GUI线程调用这些函数可能会导致用户界面冻结。
链接
https://stackoverflow.com/questions/4949548
复制相似问题