Question

I am trying to communicate an Erlang program with a simple Qt window app through an Erlang port.

The problem is that the result from the Qt window event (on_pushButton_clicked()) shows up in Erlang port only after the window is closed and not when the button is pressed:

#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();
}

Erlang (sending a Message just does nothing here, we are interested in getting data from 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.

The result of running this and filling the text field in the window is nothing (Erlang gets nothing and just waits in the receive clause):

Only when I manually close the window Erlang says:

Received data: "window_input:hello"

So, why don't I get data from Qt into Erlang port immediately?

UPD. Solution:

The solution was to flush() the Qt's buffer:

instead of fprintf(stdout, "window_input:"); I used

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

And it worked.

P.S. However, I do not understand why this problem did not happen with testing the same Qt app in console - it returned data immediately I filled in the text field in the window (i.e. on event).

Était-ce utile?

La solution

I'm not so much experienced with C++ but seems you don't flush data from your port. (And also "~n" is not new line in C++ which is not case because you use stream mode instead line.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top