Skycoder42 / QHotkey

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

Registering before creating the first window does not work #3

Closed ArsenArsen closed 7 years ago

ArsenArsen commented 7 years ago

Hello! Using a hotkey before showing a MainWindow makes the QHotkey signals not trigger. Current workaround I've found is

    QApplication a(argc, argv);

    QCommandLineParser parser;
    parser.addHelpOption();

    QCommandLineOption h({ "b", "background" }, "Does not show the main window, starts in tray.");
    parser.addOption(h);
    parser.process(a);

    MainWindow w;
    w.show();
    QTimer::singleShot(0, [&] {
        if (parser.isSet(h)) w.hide();
    });
    return a.exec();

Which flashes the window for one event loop iteration (too short to be visible).

I assume this is a singleton not being set up issue.

The hotkeys are registered at the end of the MainWindow constructor.

Thanks for creating QHotkey! :)

Skycoder42 commented 7 years ago

Well thats a surprise. This is definitly something that should work...Wich OS are you on (please detailed, e.g. Windows 10 Creators Update)? Am I assuming correctly that your non-workaround implementation looked like this:

MainWindow w;
if (!parser.isSet(h))
    w.show();
return a.exec();
ArsenArsen commented 7 years ago

Correct. I am using Arch Linux, with Xorg and KDE Plasma 5.

Skycoder42 commented 7 years ago

A fellow Arch/KDE user! I could verify the issue. There is probably something that needs to be activated by Qt, and this only happens on the first window show. I looked around, but couldn't find anything... The registration seems to work, but the key is not grabbed. (I tested with S).

For now, this workaround seems to work:

//In main
auto tmp = new QWidget();
tmp->show();
tmp->close();
tmp->deleteLater();

But I will try to find a proper solution. If not, I will integrate this code into the X11 implementation.

Skycoder42 commented 7 years ago

Solution found! A missing call to XSync

Skycoder42 commented 7 years ago

Please check if it works for you, and close the issue

ArsenArsen commented 7 years ago

Thanks! I won't be home 'till 9PM UK so won't be able to test.

ArsenArsen commented 7 years ago

👌