2011-12-13 40 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