Erlang 端口无法正确处理 C++/Qt 回复

发布于 2024-12-21 02:17:05 字数 1769 浏览 0 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-12-28 02:17:05

我对 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 use stream mode instead line.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文