2013-12-08 200 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