Skycoder42 / QHotkey

A global shortcut/hotkey for Desktop Qt-Applications
BSD 3-Clause "New" or "Revised" License
552 stars 162 forks source link

Hotkey not working when instantiated from another class #12

Closed aknobloch closed 6 years ago

aknobloch commented 6 years ago

Hey, sorry if this is not a valid concern, I'm new to C++ as well as Qt so a bit over my head here. The example code given in the README works perfectly in the main, but placing it in a class method and invoking that from the main method fails. I thought this was odd, since the shortcut still reports as being successfully registered via the isRegistered() method.

If that's not clear, let me know and I can put up some actual code. Otherwise, this library is a godsend, since I couldn't seem to get X11 to play nice for me.

Skycoder42 commented 6 years ago

Hi,

Did you just copy the code from the main? If yes, you most likely created the hotkey on the stack. That means the hotkey gets removed again once you leave the method. This only works in main that way, as that method is only left once the application finished.

Simply create the hotkey on the heap, and it should work:

auto hotkey = new QHotkey(QKeySequence("ctrl+alt+Q"), true, this);
qDebug() << "Is Registered: " << hotkey->isRegistered();
aknobloch commented 6 years ago

That makes perfect sense, thanks a lot for the concise explanation.

On Dec 20, 2017, 5:09 AM, at 5:09 AM, Felix Barz notifications@github.com wrote:

Hi,

Did you just copy the code from the main? If yes, you most likely created the hotkey on the stack. That means the hotkey gets removed again once you leave the method. This only works in main that way, as that method is only left once the application finished.

Simply create the hotkey on the heap, and it should work:

auto hotkey = new QHotkey(QKeySequence("ctrl+alt+Q"), true, this);
qDebug() << "Is Registered: " << hotkey->isRegistered();

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/Skycoder42/QHotkey/issues/12#issuecomment-353021447

Skycoder42 commented 6 years ago

Please close the issue if this fixed it for you