|
发表于 2019-9-12 18:05:24
|
显示全部楼层
本帖最后由 xf331785508 于 2019-9-12 18:07 编辑
网上调查了下,应该是可以通过如下方式实现自动检测插入的串口设备,主要的思路是对QWidget的虚函数nativeEvent复写重新实现,然后捕获系统消息来实现的。不过,对Qt不是太熟悉,看看这个样子的添加法可行不.
微软说明链接
- bool MainInterface::nativeEvent(const QByteArray &eventType, void *message, long *result)
- {
- Q_UNUSED(eventType);
- MSG* msg = reinterpret_cast<MSG*>(message);
- int msgType = msg->message;
- if(msgType == WM_DEVICECHANGE)
- {
- qDebug() << "Recv Event " ;
- PDEV_BROADCAST_HDR lpdb = (PDEV_BROADCAST_HDR)msg->lParam;
- switch(msg->wParam)
- {
- case DBT_DEVICETYPESPECIFIC:
- {
- qDebug() << "DBT_DEVICETYPESPECIFIC " ;
- break;
- }
- case DBT_DEVICEARRIVAL:
- if (lpdb -> dbch_devicetype == DBT_DEVTYP_PORT)
- {
- SerialListGet();
- PDEV_BROADCAST_PORT_A lpdbv = (PDEV_BROADCAST_PORT_A)lpdb;
- qDebug() << "Port_Arrived and The port naem is: "<<QString( lpdbv->dbcp_name);
- }
- qDebug() << "DBT_DEVICEARRIVAL" ;
- break;
- case DBT_DEVICEREMOVECOMPLETE:
- if (lpdb -> dbch_devicetype == DBT_DEVTYP_PORT)
- {
- SerialListGet();
- }
- qDebug() << "DBT_DEVICEREMOVECOMPLETE" ;
- break;
- }
- }
- return false;
- }
复制代码 |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|