如何双向通信Qt应用程序

如何双向通信Qt应用程序,qt,qprocess,qiodevice,Qt,Qprocess,Qiodevice,我想在我的Qt应用程序中创建双向通信。我想使用QProcess来完成此操作。我正在从根应用程序成功调用子应用程序并发送测试数据,没有任何错误,但我无法在子应用程序中接收任何数据。我将非常感激任何帮助。我正在使用Qt4.7.1。下面是我的测试代码: 根应用程序: InterProcess::InterProcess(QObject *parent) : QProcess(parent) { process = new QProcess(this); process->star

我想在我的Qt应用程序中创建双向通信。我想使用QProcess来完成此操作。我正在从根应用程序成功调用子应用程序并发送测试数据,没有任何错误,但我无法在子应用程序中接收任何数据。我将非常感激任何帮助。我正在使用Qt4.7.1。下面是我的测试代码:

根应用程序:

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;
}
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));
}
进程间::进程间(QObject*parent):QProcess(parent) { 流程=新的QProcess(此); 进程->启动(myChildApp); 进程->waitForStarted(); 进程->设置当前写入通道(QProcess::StandardOutput); 进程->写入(“测试”); 连接(进程,信号(错误(QProcess::ProcessError)),此,插槽(错误(QProcess::ProcessError)); 连接(进程,信号(readyReadStandardError()),此,插槽(readyReadStandardError()); 连接(进程、信号(readyReadStandardOutput())、此、插槽(readyReadStandardOutput()); QByteArray进程间::读取() { QByteArray readBuffer=进程->readAllStandardOutput(); 返回读缓冲区; } 无效进程间::错误(QProcess::ProcessError错误) {
qDebug()在本地,使用UDP非常方便和高效

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);
  }}

在本地,使用UDP非常方便和高效

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);
  }}

很难用一个答案解释所有的错误,所以只要看看代码,问问你们是否还有问题。 下面是使用QProcess作为IPC的示例

这是您的主进程,它创建附加进程并连接到其信号

myapplication.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
class InterProcess : public QProcess {
    Q_OBJECT
public:
    explicit InterProcess(QObject *parent = nullptr);
signals:
    void interProcessSignal();
private slots:
    void onMainApplicationSignal();
};
这是进程间类的示例实现:

进程间.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
class InterProcess : public QProcess {
    Q_OBJECT
public:
    explicit InterProcess(QObject *parent = nullptr);
signals:
    void interProcessSignal();
private slots:
    void onMainApplicationSignal();
};
进程间.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() {}
#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() {}
#包括“InterProcess.h”
#包括“MyApplication.h”
进程间::进程间(QObject*parent):QProcess(parent){
如果(家长){
自动myApp=QoObject\u cast(父级);
如果(myApp){
连接(myApp,信号(mainApplicationSignal()),
这是插槽(onMainApplicationSignal());
}
}
}
void进程间::onMainApplicationSignal(){}

很难用一个答案解释所有的错误,所以只要看看代码,问问自己是否还有问题。 下面是使用QProcess作为IPC的示例

这是您的主进程,它创建附加进程并连接到其信号

myapplication.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
class InterProcess : public QProcess {
    Q_OBJECT
public:
    explicit InterProcess(QObject *parent = nullptr);
signals:
    void interProcessSignal();
private slots:
    void onMainApplicationSignal();
};
这是进程间类的示例实现:

进程间.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
class InterProcess : public QProcess {
    Q_OBJECT
public:
    explicit InterProcess(QObject *parent = nullptr);
signals:
    void interProcessSignal();
private slots:
    void onMainApplicationSignal();
};
进程间.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() {}
#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() {}
#包括“InterProcess.h”
#包括“MyApplication.h”
进程间::进程间(QObject*parent):QProcess(parent){
如果(家长){
自动myApp=QoObject\u cast(父级);
如果(myApp){
连接(myApp,信号(mainApplicationSignal()),
这是插槽(onMainApplicationSignal());
}
}
}
void进程间::onMainApplicationSignal(){}

?;)嗯……我会试试。我不确定我是否理解您在儿童应用程序中尝试执行的操作。您创建了一个
QProcess
,并修复了一些连接等。但您从未将其与任何进程相关联。如果我错过了一些明显的内容,请道歉。在儿童应用程序中,我想从根应用程序读取消息。我在这里做错了什么?它必须解决Windows和Linux。我以前读过qt文档,在我的情况下,我应该用它来与QProcess或TCP/IP通信。共享内存对我来说不是最好的解决方案。?;)嗯……我会试试。我不确定我是否理解您在子应用程序中尝试做什么。您创建了一个
QProcess
并修复了一些连接等。但您从未将其与任何进程关联ess。如果我错过了一些明显的内容,我深表歉意。在child应用程序中,我想从根应用程序读取消息。我在这里做错了什么?它必须在Windows和Linux上工作。我以前阅读过qt文档,在我的情况下,我应该使用它来通信QProcess或TCP/IP。共享内存对我来说不是最好的解决方案。答案不能完全基于脱机资源使用本地UDP或TCP非常方便。非常感谢。答案不能完全基于脱机资源。使用本地UDP或TCP非常方便。非常感谢。它很有效!您的示例改变了我的想法。在我的情况下,更好的解决方案是使用QNetwork类在应用程序之间创建TCP/IP双向通信。非常感谢。@user8510613我想是的是的。我记不清楚了,但在我写这个答案之前,我用msys2和mingw64在windows上检查了这段代码。这很有效!你的例子改变了我的想法。在我的情况下,更好的解决方案是使用QNetwork类在应用程序之间创建TCP/IP双向通信。非常感谢。@user8510613我想是的。我记不清楚了,但在我写这个问题之前答案是我用msys2和mingw64在windows上检查了这段代码。