Erlang 端口无法正确处理 C++/Qt 回复
我正在尝试通过 Erlang 端口与简单的 Qt 窗口应用程序通信 Erlang 程序。
问题是,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();
}
Erlang(发送消息什么也不做)在这里,我们感兴趣的是从 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.
运行此命令并填充窗口中的文本字段的结果是什么(Erlang 没有得到任何内容,只是在 receive
子句中等待):
仅当我手动关闭窗口 Erlang 说:
Received data: "window_input:hello"
那么,为什么我不立即将数据从 Qt 获取到 Erlang 端口呢?
UPD。解决方案:
解决方案是flush() Qt 的缓冲区:
而不是fprintf(stdout, "window_input:");
我使用了
cin >> c;
cout << c;
cout.flush();
它并且有效。
PS 但是,我不明白为什么在控制台中测试相同的 Qt 应用程序时没有发生这个问题 - 我在窗口中填写文本字段(即在事件中)时它立即返回数据。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对 C++ 没有太多经验,但似乎你没有从端口刷新数据。 (而且
"~n"
不是 C++ 中的新行,这不是情况,因为您使用stream
模式而不是line
。)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 usestream
mode insteadline
.)