当前位置: 首页 > news >正文

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();}
}
http://www.lryc.cn/news/406323.html

相关文章:

  • C语言实例-约瑟夫生者死者小游戏
  • 算法类学习笔记 ———— 红绿灯检测
  • git命令使用详细介绍
  • WebStorm中在Terminal终端运行脚本时报错无法加载文件进行数字签名。无法在当前系统上运行该脚本。有关运行脚本和设置执行策略的详细信息,请参阅
  • 【Java题解】以二进制加法的方式来计算两个内容为二进制数字的字符串相加的结果
  • docker -v 到底和那个一样?type=volume还是type=bind的解释
  • linux自动化构建工具--make/makefile
  • 学习记录——day15 数据结构 链表
  • vue3实现在新标签中打开指定的网址
  • Qt基础 | QSqlTableModel 的使用
  • RPA软件-影刀使用
  • HarmonyOS NEXT零基础入门到实战-第四部分
  • vue2获取视频时长
  • Linux中进程的控制
  • 【源码】Sharding-JDBC源码分析之JDBC
  • Java | Leetcode Java题解之第273题整数转换英文表示
  • 数据结构之深入理解简单选择排序:原理、实现与示例(C,C++)
  • 使用vscode搜索打开的文件夹下的文件
  • 力扣778.水位上升的泳池中游泳
  • Nacos-2.4.0最新版本docker镜像,本人亲自制作,部署十分方便,兼容postgresql最新版本17和16,奉献给大家了
  • Halcon机器视觉15种缺陷检测案例_9找出所有网格顶点的位置
  • w30-python02-pytest入门
  • WPF+Mvvm项目入门完整教程-仓储管理系统(二)
  • SkyWalking入门搭建【apache-skywalking-apm-10.0.0】
  • exo项目目录架构
  • mysql中where与on区别
  • filebeat把日志文件上传到Es中配置(ES7版本)
  • Vue Router基础
  • Apache压测工具ab(Apache Bench)工具的下载安装和使用示例
  • IPIDEA与Python爬虫:联手解锁全球电商数据宝库