2011-12-13 35 views
1

我想通過Erlang端口與簡單的Qt窗口應用程序通信Erlang程序。Erlang端口無法正確處理C++/Qt的答覆

的問題是,從Qt的窗口事件(on_pushButton_clicked())的結果中的Erlang端口示出了窗口關閉之後才而不是當該按鈕被按下:

#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include "stdio.h" 
#include "choosefileform.h" 

#include <iostream> 
using namespace std; 


MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 

    ui->setupUi(this); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::on_pushButton_clicked() 
{ 

    fprintf(stdout, "window_input:"); 
    printf(ui->lineEdit->text().toAscii()); 
printf("~n"); 


    ChooseFileForm* fn = new ChooseFileForm(); 

    this->close(); 
    fn->show(); 
} 

二郎(發送一則短消息只是這裏什麼都不做,我們感興趣的是獲得來自Qt的數據):

connect(Message) -> 
    Cmd = "./myqtwindowapp \n", 
    Port = open_port({spawn,Cmd}, [stream,use_stdio,exit_status]), 
    Payload = string:concat(Message, "\n"), 
    erlang:port_command(Port, Payload), 
    receive 
     {Port, {data, Data}} -> 
      ?DBG("Received data: ~p~n", [Data]), 
     Other -> 
      io:format("Unexpected data: ~p~n", [Other]) 
    after 15000 -> 
      ?DBG("Received nothing~n", []) 
    end. 

運行這一點,並在窗口填寫文本字段的結果是什麼(二郎得到什麼,只是等待的receive子句中):

只有當我手動關閉該窗口二郎說:

Received data: "window_input:hello" 

那麼,爲什麼不我立刻得到Qt的數據到二郎口?

UPD。解決方案:

的解決方案是刷新()的Qt的緩衝:中

代替fprintf(stdout, "window_input:");我用

cin >> c; 
cout << c; 
cout.flush(); 

和它的工作。

P.S.但是,我不明白爲什麼在控制檯中測試相同的Qt應用程序時沒有發生這個問題 - 它立即返回數據,我填入窗口中的文本字段(即事件)。

回答

3

我沒有那麼多的C++經驗,但似乎你沒有從你的端口刷新數據。 (並且"~n"在C++中也不是新行,因爲您使用的是stream模式,而不是line。)

+1

是的,您需要顯式刷新數據。 – rvirding 2011-12-14 04:07:38