Weird Qt behavior crashing my application

2019-09-19 08:21发布

Normally when I have a problem in Qt, it is usually due to me not understanding something but this problem is just illogical.

I have this function that handle hotkey's event:

bool MainWidget::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
    MSG* msg = reinterpret_cast<MSG*>(message);
    if (msg->message == WM_HOTKEY)
    {
        qDebug() << "It works";   // IF I REMOVE THIS LINE, THE APP CRASHES.
        togglevisibility(false);
    }
}

It works perfectly well but if I remove the qDebug() line, then the app crashes as soon as it starts running. Nothing gets displayed. The code in the constructor runs but it seems it crashes as soon as it gets to this function.

In my constructor of my main widget, I have this line to register my hotkey:

if (!RegisterHotKey(HWND(this->winId()), 1, MOD_CONTROL | MOD_SHIFT | 0x4000, 0x41)) qDebug() << ("Hotkey failed");

Adding any other line instead of the qDebug doesn't make a difference. (e.g delete msg). Removing the line 'togglevisility' doesn't make a difference which rules out that function.

If I move the qDebug line before the if statement will NOT crash the application. It would seem that the qDebug line is required somehow which doesn't make any sense at all.

If anyone can make sense of this, please let me know. I rather not leave qDebug lines in my application.

标签: qt crash
1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-19 08:56

I found out how to stop the application crashing and removing the qDebug. the Qt nativeEvent event has to return a bool. Adding the line return NULL will stop the crashing.

However I cannot explain why qDebug prevented it from crashing before.

查看更多
登录 后发表回答