c++ - Communicate with two instances of Qt program -


i used in qt program code avoid opening second instance:

#include "mainwindow.h" #include <qapplication> #include <qsharedmemory> #include <qdebug>  int main(int argc, char *argv[]) {     qapplication a(argc, argv);          const char* mem_key = "42";          qsharedmemory sharedmem(mem_key);          if (sharedmem.create(1024))          {             qdebug() << "create shared memory";         }          else          {             if (sharedmem.error() == qsharedmemory::alreadyexists)              {                 qwarning() << "already create. exiting process";                 return 1;             }         }         mainwindow w;         w.setwindowflags(qt::mswindowsfixedsizedialoghint);         w.show();         return a.exec(); 

it works (this code block opening second instance of aplication [it automatically close]), want send message or signal opened moment second instance first instance perform ex. maximalize window of first instance. tell me how simply?

you can use qtsingleapplication purpose. example:

int main(int argc, char *argv[]) {     qtsingleapplication a(argc, argv);     if (a.isrunning())     {         a.sendmessage("42");         qwarning() << "already create. exiting process";         return 1;     }      mainwindow w;     a.setactivationwindow(&w);     qobject::connect(&a, signal(messagereceived(qstring))                      , &w, slot(onmessagereceived(qstring)));     w.show();      int ret = a.exec();     qobject::disconnect(&a, 0, &w, 0);     return ret; } 

...

void mainwindow::onmessagereceived(const qstring &message) {     // stuff } 

Comments