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();
    }
}
相关推荐
乌萨奇也要立志学C++8 分钟前
【Linux】基本指令详解(一) 树状文件结构、家目录、绝对/相对路径、linux文件类型
linux
marconiho10 分钟前
FRP Ubuntu 服务端 + MacOS 客户端配置
linux·ubuntu·macos
DARLING Zero two♡14 分钟前
【Linux操作系统】简学深悟启示录:Linux权限
linux·运维·服务器
越城14 分钟前
C++类与对象(上)
开发语言·c++
is081530 分钟前
vim扩展
linux·编辑器·vim
泽020239 分钟前
C++之哈希表的基本介绍以及其自我实现(开放定址法版本)
c++
序属秋秋秋42 分钟前
《C++初阶之STL》【泛型编程 + STL简介】
开发语言·c++·笔记·学习
特种加菲猫2 小时前
构建完整工具链:GCC/G++ + Makefile + Git 自动化开发流程
linux·笔记·git·自动化
点云SLAM3 小时前
二叉树算法详解和C++代码示例
数据结构·c++·算法·红黑树·二叉树算法
李少兄5 小时前
CentOS系统下前后端项目部署攻略
linux·运维·centos