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

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

需求:

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

.h

#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

#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();
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值