2011-02-09 119 views
2

我正試圖從qprocess讀取和寫入現在。我做了一個小測試程序,它能夠輸入並在一個循環中重新顯示它在屏幕上。下面是從Qt的QProcess讀取和寫入

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); 

    } 

} 

回答

1

從DOC:

QIODevice中的某些亞類,例如 如與QTcpSocket和QProcess中,是 異步。這意味着,當控制返回到事件循環時,總是可以立即返回諸如write()或read() 之類的函數,而與設備本身的 通信可能發生。 了QIODevice提供 功能,使您可以強制 這些操作的執行 立即,而阻斷 調用線程,並沒有進入 事件循環。

...

waitForBytesWritten() - 直到一個數據有效載荷 被寫入設備,此功能 暫停在調用的 線程操作。

...

調用從主, GUI線程這些功能,可能會導致你的用戶界面 凍結。

link

+0

是啊,但我追趕的信號,所以我不認爲應該的問題。但也許我對此有所瞭解。無論如何,我把一個tcd-> waitForBytesWritten();在我的代碼中,仍然沒有。編輯:我有一個按鈕,將輸入發送到我的代碼,所以它是在信號啓動後,仍然沒有。 – 2011-02-09 20:38:47