0

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.

David
  • 47
  • 9
  • What happens if you add a `return false;` at the end of the function ? – alexisdm May 29 '14 at 21:56
  • Adding a return false, return true or return null effectively stops the application from crashing. As pointed out by another comment below, return NULL should be avoided. – David May 31 '14 at 19:39

1 Answers1

0

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.

David
  • 47
  • 9
  • I'd suggest using 'false' instead of NULL (and avoid NULL whatsoever), and check if passed in pointer is valid before using. – hauron May 29 '14 at 22:09
  • Did you try cleaning the project and rebuilding? – Quaxton Hale May 31 '14 at 07:16
  • I replaced the NULL by TRUE/FALSE depending on the whether the function received a matching event or not, but the return is not being used in my case. – David May 31 '14 at 19:40
  • Cleaning the project and rebuilding does not make a difference. – David May 31 '14 at 19:41