2

I want to create two-way communicate beetwen my Qt Apps. I want to use QProcess to do this. I'm calling sucesfully child app from root app and sending test data without any erro, but I can't recive any data in child app. I'll be gratefull for any help. I'm using Qt 4.7.1. Below my test code:

Root app:

InterProcess::InterProcess(QObject *parent) : QProcess(parent)
{
    process = new QProcess(this);
    process->start(myChildApp);
    process->waitForStarted();
    process->setCurrentWriteChannel(QProcess::StandardOutput);
    process->write("Test");

    connect( process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)) );
    connect( process, SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()) );
    connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()) );     

QByteArray InterProcess::read()
{
    QByteArray readBuffer = process->readAllStandardOutput();
    return readBuffer;
}

void InterProcess::error( QProcess::ProcessError error )
{
    qDebug() << "Error!";
    qDebug() << error;
}

void InterProcess::readyReadStandardError()
{
    qDebug() << "Ready to read error.";
    qDebug() << process->readAllStandardError();
}

void InterProcess::readyReadStandardOutput()
{
    qDebug() << "The output:";
    QByteArray readBuffer = process->readAllStandardOutput();
    qDebug() << readBuffer;
}

Child app:

InterProcess::InterProcess(QObject *parent) : QProcess(parent)
{
    process = new QProcess();
    process->setCurrentReadChannel(QProcess::StandardOutput);

    connect( process, SIGNAL(readyRead()), this, SLOT(readyReadStandardOutput()));
    connect( process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)) );
    connect( process, SIGNAL(readyReadStandardError()), this, SLOT(readyReadStandardError()) );
    connect( process, SIGNAL(readyReadStandardOutput()), this, SLOT(readyReadStandardOutput()) );

    process->waitForReadyRead(5000);
}

void InterProcess::readyReadStandardError()
{
    qDebug() << "Ready to read error.";
    qDebug() << process->readAllStandardError();
    setText("REady error");
}

void InterProcess::readyReadStandardOutput()
{
    setMessage("2");
    qDebug() << "The output:";
    QByteArray readBuffer = process->readAllStandardOutput();
    qDebug() << readBuffer;
}
void InterProcess::error( QProcess::ProcessError error )
{
    qDebug() << "Error!";
    qDebug() << error;
    setText(QString(error));
}
kluszon
  • 375
  • 5
  • 19
  • 1
    [`QLocalSocket`](http://doc.qt.io/qt-5/qlocalsocket.html)? ;) – Dmitry Sazonov Mar 21 '18 at 15:07
  • Hmm... I'll try it. – kluszon Mar 21 '18 at 16:35
  • I'm not sure I understand what you're trying to do in the child app. You create a `QProcess` and fix up some connections etc,. but you never associate it with any process. Apologies if I've missed something obvious. – G.M. Mar 21 '18 at 17:52
  • http://doc.qt.io/qt-5/ipc.html – Andrey Semenov Mar 21 '18 at 20:50
  • In child app I want to read message from root app. What I doing wrong here? It must work on Windows and Linux. I read qt doc befor and in my case I should use to communicate QProcess or TCP/IP. Shared memory it isn't best solution for me. – kluszon Mar 22 '18 at 07:41

2 Answers2

3

It's very hard to explain in one answer all mistakes, so just look at code and ask if you still got problems. Here is example of using QProcess as IPC.

This is your main process, that creates additional process and connects to its signals

MyApplicaiton.h

#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H
#include <QApplication>

class InterProcess;
class MyApplication : public QApplication {
    Q_OBJECT
public:
    MyApplication(int &argc, char **argv);
signals:
    void mainApplicationSignal();
private slots:
    void onInterProcessSignal();
private:
    InterProcess *mProcess;
};
#endif // MYAPPLICATION_H

MyApplicaiton.cpp

#include "MyApplication.h"
#include "InterProcess.h"

MyApplication::MyApplication(int &argc, char **argv) : QApplication(argc, argv) {
    mProcess = new InterProcess(this);
    connect(mProcess, SIGNAL(interProcessSignal()),
            this, SLOT(onInterProcessSignal()));
    mProcess->start();
}

void MyApplication::onInterProcessSignal() {}

This is example implementation of your interProcess class:

InterProcess.h

class InterProcess : public QProcess {
    Q_OBJECT
public:
    explicit InterProcess(QObject *parent = nullptr);
signals:
    void interProcessSignal();
private slots:
    void onMainApplicationSignal();
};

InterProcess.cpp

#include "InterProcess.h"
#include "MyApplication.h"


InterProcess::InterProcess(QObject *parent) : QProcess(parent) {
    if(parent) {
        auto myApp = qobject_cast<MyApplication *>(parent);
        if(myApp) {
            connect(myApp, SIGNAL(mainApplicationSignal()),
                    this, SLOT(onMainApplicationSignal()));
        }
    }
}

void InterProcess::onMainApplicationSignal() {}
Andrey Semenov
  • 901
  • 11
  • 17
  • It work! Your example changed my mind. In my case better solution will be use QNetwork class to create TCP/IP two way communication between apps. Thanks a lot. – kluszon Mar 22 '18 at 12:08
  • @user8510613 i think yes. I do not remember it clear but before i wrote this answer i checked this code on windows with msys2 and mingw64. – Andrey Semenov May 06 '19 at 14:54
0

Locally, using UDP is very convenient and efficient

void Server::initSocket() {
  udpSocket = new QUdpSocket(this);
  udpSocket->bind(QHostAddress::LocalHost, 7755);
  connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));}


void Server::readPendingDatagrams(){
  while (udpSocket->hasPendingDatagrams()) {
      QByteArray datagram;
      datagram.resize(udpSocket->pendingDatagramSize());
      QHostAddress sender;
      quint16 senderPort;

      udpSocket->readDatagram(datagram.data(), datagram.size(),
                              &sender, &senderPort);

      processTheDatagram(datagram);
  }}
tujiaw
  • 81
  • 3