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();
    }
}
相关推荐
鸠摩智首席音效师几秒前
如何在 Vim 中使用交换文件恢复文件 ?
linux·编辑器·vim
高旭的旭11 分钟前
Linux V4L2框架详解:Camera软件架构与驱动实现
linux·嵌入式·camera·v4l2
Raymond运维17 分钟前
MySQL包安装 -- SUSE系列(SUSE资源库安装MySQL)
linux·运维·数据库·mysql
你的冰西瓜27 分钟前
C++动态规划入门指南——助力CSP竞赛夺冠
c++·动态规划
九皇叔叔1 小时前
Linux 系统配置 NTP 服务:轻松同步阿里云时间服务器
linux·服务器·阿里云
東雪蓮☆2 小时前
K8S 概念、安装与核心工作机制详解
linux·运维·云原生·容器·kubernetes
西阳未落2 小时前
LeetCode——双指针
c++·算法
rain bye bye2 小时前
vim 中设置高亮
linux·编辑器·vim
第四维度42 小时前
【全志V821_FoxPi】6-3 GC2083 MIPI摄像头适配
linux·tina·v821·gc2083
liulilittle2 小时前
Linux 内核网络调优:单连接大带宽吞吐配置
linux·运维·服务器·网络·信息与通信·通信