从静态功能问题发出信号

Emit signal from static function issue

我想在 wlan 连接从静态 notificationCallBack 函数注册时发出信号。

代码:

WirelessConnect.h

static void WINAPI notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context);

WirelessConnect.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 关于 Test class 中的 wlan 连接?

上面 link 的解决方案不起作用:

1 方法:notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context) 函数只需要 2 个参数 PWLAN_NOTIFICATION_DATA wlanDataPVOID context 而不是对 class 作为参数的引用

2 方法:导致错误: C3867: 'WirelessConnect::apConnectionComplete': non-standard syntax; use '&' to create a pointer to member in the connect: connect(wirelessAPConnect, WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);

我已经用 2 方法修复了编译错误,但它什么也没发出。

代码:

WirelessConnect.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);

Test.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(); 应用程序可以运行,但 signal/slot 没有执行。

已编辑:

WirelessConnect class 还管理其他非静态 wlan 函数,这些函数连接到信号和槽。所以我不能删除 wirelessAPConnect 对象并只使用静态对象。我也在 Test 中初始化了 static WirelessConnect wirelessConnectObj; 并连接它:

connect(&wirelessConnectObj, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection);
connect(&wirelessConnectObj, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);

输出相同 - 没有。我认为,这是因为我创建了 WirelessConnect class 的 2 个不同的静态实例。我只需要 WirelessConnect 的一个静态实例,它应该是全局的以发送信号并将它们连接到 Test.

我已经解决了这个问题:

我将 this 设置为函数:

WlanRegisterNotification(hClient, WLAN_NOTIFICATION_SOURCE_ALL, TRUE, notificationCallBack, this, NULL, &dwPrevNotif);

void WINAPI WirelessConnect::notificationCallBack(PWLAN_NOTIFICATION_DATA wlanData, PVOID context)
{
    WirelessConnect *pThis = (WirelessConnect*) context;
    switch (wlanData->NotificationCode) {
        case wlan_notification_acm_connection_complete:
            pThis->apConnectionComplete();
            qDebug() << "Connected!";
        break;

        case wlan_notification_acm_connection_attempt_fail:
            pThis->apConnectionFailed();
            qDebug() << "Failed!";
        break;

        default:
            break;
    }
}

然后将其连接为非静态实例:

connect(wirelessAPConnect, &WirelessConnect::apConnectionComplete, this, &Test::setAPConnection); 
connect(wirelessAPConnect, &WirelessConnect::apConnectionFailed, this, &Test::setAPConnectionFailed);

所以我不再需要静态全局实例,它现在可以工作了。

已编辑:

经过一些诊断(connecting/disconnecting 来自接入点),该解决方案导致应用程序崩溃。