我想在wlan
从静态notificationCallBack
函数注册连接时发出信号。
代码:
无线连接.h
static void WINAPI notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context);
无线连接.cpp
DWORD dwResult = 0;
dwResult = WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, NULL, NULL, &dwPrevNotif);
void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
{
context = NULL;
switch (wlanData->NotificationCode) {
case wlan_notification_acm_connection_complete:
emit apConnectionComplete();
qDebug() << "Connected!";
break;
case wlan_notification_acm_connection_attempt_fail:
emit apConnectionFailed();
qDebug() << "Failed!";
break;
default:
break;
}
}
测试.cpp:
WirelessConnect *wirelessAPConnect = new WirelessConnect();
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, setAPConnection);
connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, setAPConnectionFailed);
问题是error: C2355: 'this': can only be referenced inside non-static member functions or non-static data member initializers
。
那么如何从static
函数中发出信号,例如QMessageBox
在课堂上显示有关 wlan 连接的信息Test
?
上面链接中的解决方案不起作用:
1 方法:notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
函数只需要2个参数PWLAN_NOTIFICATION_DATA wlanData
,PVOID context
而不是对类的引用作为参数
2 方法:导致错误:C3867: 'WirelessConnect::apConnectionComplete': non-standard syntax; use '&' to create a pointer to member
在连接中:
connect(wirelessAPConnect, WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
我已经用 2 方法修复了编译错误,但它什么也没发出。
代码:
无线连接.cpp
static WirelessConnect wirelessConnectObj;
void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
{
context = NULL;
switch (wlanData->NotificationCode) {
case wlan_notification_acm_connection_complete:
wirelessConnectObj.apConnectionComplete();
qDebug() << "Connected!";
break;
case wlan_notification_acm_connection_attempt_fail:
wirelessConnectObj.apConnectionFailed();
qDebug() << "Failed!";
break;
default:
break;
}
}
dwResult = WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, NULL, NULL, &dwPrevNotif);
测试.cpp
WirelessConnect *wirelessAPConnect = new WirelessConnect();
connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
没有编译错误并且setAPConnection
不执行。
我认为static WirelessConnect wirelessConnectObj;
是 NULL 并且它什么也不执行。当我使用静态 WirelessConnect *wirelessConnectObj; 作为应用程序连接到接入点后的指针,然后应用程序崩溃。
初始化static WirelessConnect *wirelessConnectObj = new WirelessConnect();
应用程序时工作但没有信号/插槽执行。
编辑:
WirelessConnect
wlan
类还管理连接到信号和槽的其他非静态函数。所以我不能删除wirelessAPConnect
对象而只使用静态对象。我也初始化static WirelessConnect wirelessConnectObj;
并Test
连接它:
connect(&wirelessConnectObj, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
connect(&wirelessConnectObj, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);
输出是一样的——什么都没有。我认为,这是因为我创建了 2 个不同的静态WirelessConnect
类实例。我只需要一个静态实例,WirelessConnect
它应该是全局的,可以发送信号并将它们连接到Test
.