2013-12-08 152 views
0

我想用QProcess从GUI启动mysql。我试过以下内容:QProcess GUI不冻结

QStringList arguments; 
arguments << QString("-u%1").arg("myaccount")<< QString("-p%2").arg("password"); 

QProcess *mysql = new QProcess; 
mysql->setReadChannelMode(QProcess::ForwardedChannels); 
mysql->execute("mysql", arguments); 

if(mysql->waitForReadyRead(-1)) 
    qDebug(mysql->readAllStandardOutput()); 

但是,有一个很大的问题,因为它在Qt文档中提到,它会冻结。 我该如何解决这个问题?许多人建议使用QThread,但我不知道该怎么做? 预先感谢!

回答

1

问题是您调用QProcess::execute()函数并等待,直到该过程完成。如果你需要避免冻结,您可以使用readyReadStandardOutput()信号,并执行以下操作:

[..] 
connect(mysql, SIGNAL(readyReadStandardOutput()), this, SLOT(readOutput())); 
mysql->start("mysql", arguments); 
if (!mysql->waitForStarted()) { 
    // report error 
} 
+0

恩,谢谢!另外,如何在mysql密码或用户帐户输入错误时提示警告消息框。连接readyReadStandardOutput()信号后,我发现这个问题会有问题。你能帮我吗? – elgolondrino

+0

@elgolondrino,我认为这取决于'mysql'报告关于不正确的用户名/密码输入:退出与错误代码,打印出错误信息到标准输出或标准错误? – vahancho