Skycoder42 / QHotkey

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

Why doesn't QHotkey work on the widget? #79

Open xx-hex-xx opened 1 year ago

xx-hex-xx commented 1 year ago

Hello! I can't get QHotkey to work on the widget. If I use the example code from ReadMe in EntryPoint.cpp, the hotkey works but if I use the same code in WMain.cpp it hotkey no longer works.

EntryPoint.cpp --> WORKS

int main(int argc, char* argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    qputenv("QT_AUTO_SCREEN_SCALE_FACTOR", "1");
    qputenv("QT_FONT_DPI", "96");

    QApplication qApplication(argc, argv);

    QHotkey hotkey(QKeySequence("Ctrl+Q"), true, &qApplication);
    QObject::connect(&hotkey, &QHotkey::activated, qApp, [&]() { qApp->quit(); });

    WMain wMain;
    wMain.show();

    return qApplication.exec();
}

WMain.cpp --> DOESNT WORK

WMain::WMain(QWidget* parent) : QMainWindow(parent, Qt::FramelessWindowHint)
{
    this->UI_WMain.setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);

    this->qGraphicsDropShadowEffect->setBlurRadius(10);
    this->qGraphicsDropShadowEffect->setColor(QColor(0, 0, 0));
    this->qGraphicsDropShadowEffect->setOffset(0);
    this->UI_WMain.widgetMainFrameless->setGraphicsEffect(this->qGraphicsDropShadowEffect);

    QHotkey hotkey(QKeySequence("Ctrl+Q"), true, this);
    QObject::connect(&hotkey, &QHotkey::activated, qApp, [&]() { qApp->quit(); });
}

What am I doing wrong?