Linux中利用消息队列给两个程序切换显示到前台

消息队列--两个进程间的通信

需求:

  • 1.在Linux系统中
  • 2.两个ui界面的程序切换,一个显示,另一个隐藏

.h

cpp 复制代码
#ifndef PROGRAMWINDOWSWITCH2_H
#define PROGRAMWINDOWSWITCH2_H

#include <QObject>
#include <QThread>
#include <QMainWindow>

struct Message{
    long msgType;
    int msgText[2];
};

class MessageQueueReceiver : public QThread
{
    Q_OBJECT
public:
    MessageQueueReceiver(int msgid,QObject *parent = nullptr,int type = 1):QThread(parent),msgid(msgid),curWindowType(type){}

signals:
    void messageReceived(const int &message);

protected:
    void run() override;

private:
    int msgid;
    int curWindowType;
};

class programwindowswitch2 : public QObject
{
    Q_OBJECT
public:
    enum WindowType{
        ConfigWindow = 1,
        ControlWindow = 2
    };
    explicit programwindowswitch2(QObject *parent = nullptr,WindowType type = ConfigWindow);

signals:
    void showWindowSignal(void);
    void ChangedWindow(void);
public slots:
    void vSwitchOtherWindow(bool flag= true);

private slots:
    void handleMessage(const int &message);

private:
    int msgid;
    MessageQueueReceiver *m_receiver;

    QWidget *pMainWidget;
    WindowType curWindowType;
};

#endif // PROGRAMWINDOWSWITCH2_H

.cpp

cpp 复制代码
#include "programwindowswitch2.h"
#include <QMutex>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <iostream>

#include <QDebug>

programwindowswitch2::programwindowswitch2(QObject *parent,WindowType type) : QObject(parent),curWindowType(type)
{
    pMainWidget = static_cast<QMainWindow*>(parent);

    key_t key = ftok("myMessage",35);
    msgid = msgget(key,0666 | IPC_CREAT);

    m_receiver = new MessageQueueReceiver(msgid,this,curWindowType);
    connect(m_receiver,&MessageQueueReceiver::messageReceived,this,&programwindowswitch2::handleMessage);
    m_receiver->start();
}

void MessageQueueReceiver::run()
{
    Message msg;
    while (true) {
        if(msgrcv(msgid,&msg,sizeof(msg),curWindowType,0) > 0){
            emit messageReceived(msg.msgText[0]);
        }
    }
}

void programwindowswitch2::vSwitchOtherWindow(bool flag)
{
    Message msg;

    if(pMainWidget == nullptr)
        return;

    switch (curWindowType) {
    case ConfigWindow:
        msg.msgType = 2;
        msg.msgText[0] = flag;
        break;
    case ControlWindow:
        msg.msgType = 1;
        msg.msgText[0] = flag;
        break;
    default:
        return;
        break;
    }

    msgsnd(msgid,&msg,sizeof(msg),0);
}

//show windows
void programwindowswitch2::handleMessage(const int &message)
{
    bool show_flag = false;

    if(pMainWidget == nullptr)
        return;

    switch (curWindowType) {
    case ConfigWindow:
        if(message == 1){
            show_flag = true;
        }
        break;
    case ControlWindow:
        if(message == 1){
            show_flag = true;
        }
        break;
    default:
        break;
    }
    if(show_flag){
        emit ChangedWindow();
        pMainWidget->show();
        vSwitchOtherWindow(false);
    }
    else {
        pMainWidget->hide();
    }
}
相关推荐
子豪-中国机器人10 小时前
枚举算法和排序算法能力测试
开发语言·c++·算法
1白天的黑夜110 小时前
栈-844.比较含退格的字符串-力扣(LeetCode)
c++·leetcode·
优秀的老黄11 小时前
Docker部署RabbitMQ
linux·运维·docker·中间件·容器·centos·rabbitmq
Lin_Aries_042111 小时前
容器使用卷
linux·运维·docker·云原生·容器·eureka
林夕忆梦_猫11 小时前
初识C++
开发语言·c++
chxin1401611 小时前
openCV3.0 C++ 学习笔记补充(自用 代码+注释)---持续更新 四(91-)
c++·opencv·计算机视觉
lightqjx11 小时前
【C++】string类 模拟实现
java·开发语言·c++
Liang_GaRy11 小时前
心路历程-Linux的特殊权限
linux·运维·服务器
0wioiw011 小时前
Docker(⑤Kali Linux-HexStrike AI安装)
linux·服务器
青草地溪水旁11 小时前
C/C++ 标准库中的 `strspn` 函数
c语言·c++