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


i need advice on program i'm coding right now. let me first present is.

design

i'm designing man-machine interface (mmi). in mmi, there 2 core elements:

  • the mainwindow:

    mainwindow mmi

this mainwindow base of everything. important: here, launch in thread server receives data client. data important next element.

  • supervision

    supervision ihm

this window contains qtablewidget, goal show in pseudo-real time data received on server in thread of mainwindow.

the problem

the server in thread owned mainwindow receive structure every 10 ms. how can send these data supervision window if it's open? thinking of using shared memory, not sure this, , doesn't method have use.

some solutions

i tried implement solution of sebastian lange :

  • an emit in thread server
  • a connect in mainwindow
  • a slot in supervision

so thread server emit signal @ every frame received. how can connect in mainwindow , how supervision receive struct emit in signal?

here's code of 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 );         }     } } 

queued connections

qt makes cross thread communication easy when use queued connections.

your calls connect should use qt::queuedconnection or qt::blockingqueuedconnection connection types.

custom types in slots , signals

to use custom types (structs) in slots, signals, qvariant , properties need declare , register type make available qt dynamic type system.

in header (.hpp) use q_declare_metatype , in source (.cpp) use qregistermetatype.

worked example

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;   (int = 0; < 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 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -