gpt4 book ai didi

c++ - 服务器线程与人机界面 (MMI) 之间的通信

转载 作者:行者123 更新时间:2023-11-30 01:52:31 25 4
gpt4 key购买 nike

我需要您对我正在编写的程序的建议。首先让我向您介绍一下它是什么。

设计

我正在设计人机界面 (MMI)。在这个MMI中,有两个核心元素:

  • 主窗口:

    MainWindow MMI

这个主窗口是一切的基础。 重要:在这里,我在一个线程中启动了一个服务器,它从客户端接收数据。此数据对下一个元素非常重要。

  • 监督

    Supervision IHM

这个窗口包含一个QTableWidget,目的是在MainWindow线程中伪实时显示服务器接收到的数据。

问题

MainWindow 拥有的线程中的服务器每 10 毫秒接收一个结构。如果 Supervision 窗口打开,我如何将这些数据发送到它?我在考虑使用共享内存,但对此不太确定,也不知道我必须使用哪种方法。

一些解决方案

我尝试实现 Sebastian Lange 的解决方案:

  • 在线程服务器中发出
  • 主窗口中的连接
  • 监管空档

现在我的线程 Server 在接收到每一帧时发出一个信号。但是我如何在 MainWindow 中进行连接以及 Supervision 将如何接收信号中的 struct emit

这是我发出的代码:

MainWindow* MainWindow::m_psMainWindow = nullptr; // C++ 11 nullptr
void MainWindow::emit_signal_TrameRecu(StructureSupervision::T_StructureSupervision* ptr){
emit signal_TrameRecu(ptr);
}

void MainWindow::lancerServeur(std::atomic<bool>& boolServer){
serveur s;
StructureSupervision::T_StructureSupervision* bufferStructureRecu;
while(boolServer){
bufferStructureRecu = s.receiveDataUDP();
if(bufferStructureRecu->SystemData._statutGroundFlight != 0){
m_psMainWindow->emit_signal_TrameRecu( bufferStructureRecu );
}
}
}

最佳答案

排队连接

当您使用队列连接时,Qt 使跨线程通信变得容易。

您对 connect 的调用应该使用 Qt::QueuedConnectionQt::BlockingQueuedConnection连接类型。

插槽和信号中的自定义类型

要在插槽、信号、QVariant 和属性中使用自定义类型(结构),您需要声明注册 类型以使其可用于 Qt 动态类型系统.

在您的 header (.hpp) 中使用 Q_DECLARE_METATYPE并在您的源代码 (.cpp) 中使用 qRegisterMetaType .

实例

server.hpp

#ifndef SERVER_HPP
#define SERVER_HPP
#include <QtCore>

struct customdata
{
int id;
QDateTime tstamp;
};
Q_DECLARE_METATYPE(customdata)

class Server : public QThread
{
Q_OBJECT
public:
Server();
signals:
void sendData(const customdata& d);
protected:
virtual void run();
};

#endif

server.cpp

#include "server.hpp"

static const int customdata_metatype_id =
qRegisterMetaType<customdata>();

Server::Server() : QThread() {}

void Server::run()
{
customdata d;
d.id = 0;
for (int i = 0; i < 10; ++i)
{
d.id++;
d.tstamp = QDateTime::currentDateTime();
emit sendData(d);

sleep(1);
}
}

window.hpp

#ifndef WINDOW_HPP
#define WINDOW_HPP
#include <QtGui>
#include "server.hpp"

class Window : public QWidget
{
Q_OBJECT
public:
Window();
public slots:
void receiveData(const customdata& d);
private:
QListWidget* mList;
};

#endif

window.cpp

#include "window.hpp"

Window::Window() : QWidget(),mList(new QListWidget())
{
resize(400, 300);
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->addWidget(mList);
setLayout(mainLayout);
}

void Window::receiveData(const customdata& d)
{
QString str(QString("%1 %2").arg(d.id).arg(d.tstamp.toString()));
mList->addItem(str);
}

main.cpp

#include <QtGui>

#include "server.hpp"
#include "window.hpp"

int main(int argc, char** argv)
{
QApplication app(argc, argv);

Window win;
Server ser;

QObject::connect(
&ser, SIGNAL(sendData(customdata)),
&win, SLOT(receiveData(customdata)),
Qt::QueuedConnection);

win.show();
ser.start();

return app.exec();
}

test.pro

TEMPLATE=app
QT=core gui
HEADERS=server.hpp window.hpp
SOURCES=main.cpp server.cpp window.cpp

关于c++ - 服务器线程与人机界面 (MMI) 之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24446043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com