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

PySide6 监测设备变更事件

        在PySide6中监听系统事件,判断是否有串口设备插拔,进而当串口状态变更时,实现列表数据实时更新。

        在Qt中,可以使用 nativeEvent 接口来完成这一操作:

[virtual protected] bool QWidget::nativeEvent(const QByteArray &eventType, void *message, long *result)

This special event handler can be reimplemented in a subclass to receive native platform events identified by eventType which are passed in the message parameter.

In your reimplementation of this function, if you want to stop the event being handled by Qt, return true and set result. The result parameter has meaning only on Windows. If you return false, this native event is passed back to Qt, which translates the event into a Qt event and sends it to the widget.

Note: Events are only delivered to this event handler if the widget has a native window handle.

Note: This function superseedes the event filter functions x11Event(), winEvent() and macEvent() of Qt 4.

Platform

Event Type Identifier

Message Type

Result Type

Windows

"windows_generic_MSG"

MSG *

LRESULT

macOS

"NSEvent"

NSEvent *

XCB

"xcb_generic_event_t"

xcb_generic_event_t *

直接上代码: 

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

    def nativeEvent(self, event_type, message):
        if event_type == "windows_generic_MSG":
            msg = wintypes.MSG.from_address(message.__int__())
            if msg.message == win32con.WM_DEVICECHANGE:
                # 添加设备变更的处理

        return super(QMainWindow, self).nativeEvent(event_type, message)

http://www.dtcms.com/a/126009.html

相关文章:

  • 智慧能源管理平台:驱动电网数字化转型,引领绿色能源新时代
  • HTML应用指南:利用GET请求获取全国汉堡王门店位置信息
  • 算法思想之前缀和(二)
  • 操作系统 4.3-生磁盘的使用
  • 种田游戏的综合尝试
  • IBM Rational Software Architect安装感受及使用初体验
  • 日语学习-日语知识点小记-构建基础-JLPT-N4阶段(2):んです
  • 【ESP32S3】GATT Server service table传送数据到调试助手
  • 2025.04.05 广东汕尾两日游记
  • DNS正反向解析复习,DNS主从服务,转发服务及DNS和nginx联合案例(不断更新)
  • BERT-DDP
  • Google ADK(Agent Development Kit)简要示例说明
  • 凌科加密芯片LKT4304在充电桩上的应用
  • 【LeetCode 题解】算法:36.有效的数独
  • C++20新特性:协程
  • 一文解析2025年移动游戏用户获取策略 | 增长方法论
  • 【CF】Day30——Codeforces Round 824 (Div. 2) C + Codeforces Round 825 (Div. 2) BC1
  • 数字内容体验的核心价值是什么?
  • 【前后端】npm包mysql2的使用__nodejs mysql包升级版
  • 四、TorchRec的推理优化
  • RAG(检索增强生成)系统,提示词(Prompt)表现测试(数据说话)
  • 如何在AMD MI300X 服务器上部署 DeepSeek R1模型?
  • c++关键字new
  • CExercise_07_1指针和数组_2数组元素的逆序数组逆序(指针版 reverse_by_ptr 和下标版 reverse_arr)
  • NVIDIA H100 vs A100:新一代GPU架构性能对比分析
  • 第五章:5.1 ESP32物联网应用 - MQTT协议深度教程
  • 大厂文章阅读
  • 速盾:高防CDN节点对收录有影响吗?
  • 深入解析 Microcom:嵌入式串口调试利器
  • 代码随想录算法训练营Day27 | Leetcode 56. 合并区间、738.单调递增的数字、968.监控二叉树