Communication between server thread and man-machine interface (MMI)

I need your advice on the program that I am coding right now. Let me first introduce you to what it is.

Design

I am designing a human machine interface (MMI). There are two main elements to this MMI:

  • MainWindow:

    MainWindow MMI

This MainWindow is the foundation of everything. Important: here I run thread a server, which receives data from the client. This data is very important for the next item.

  • Control

    Supervision IHM

This window contains QTableWidget, the goal is to show in pseudo-real time the data received on the server in the MainWindow stream.

Problem

, MainWindow, 10 . Supervision, ? , , , .

Sebastian Lange:

  • MainWindow

, 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 );
        }
    }
}
+1
2

Qt .

connect Qt::QueuedConnection Qt::BlockingQueuedConnection.

โ€‹โ€‹

(structs) , , QVariant , , Qt.

(.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
+3

( Qt 3.2) QApplication::postEvent ( QCoreApplication:: postEvent). promises . promises, ++, Qt 5. . http://qt-project.org/doc/qt-5/qtconcurrent-index.html

+1

Source: https://habr.com/ru/post/1546516/


All Articles