Skycoder42 / QHotkey

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

Qml example #30

Closed IceTrooper closed 3 years ago

IceTrooper commented 4 years ago

Hi, first of all, thank you very much for this package.

I see there is QWidget example with .ui file. Can you provide an example of how to use it with .qml components? What to register in main.cpp and in what way (qmlRegisterType/setContextProperty/qmlRegisterUncreatableType) to use it in .qml files? I was wondering if that would be similar usage like Shortcuts qml components.

Thank you

EDIT: or just say in what way I can use it with Qml components (some snippet) and I can provide the same example in qml like you did in .ui in PR

Skycoder42 commented 4 years ago

Well, it's a standard Qt Object, so just using qmlRegisterType should be all you need. As long as the qml component stays in scope, the hotkey should work as per usual. For example:

// cpp
qmlRegisterTypes<QHotKey>("de.skycoder42.QHotKey", 1, 0, "HotKey");

// qml
HotKey {
    shortcut: …
    registered: true

    onActivated: {
        ...
    }
}

The only problem here is: I think QKeySequence cannot be created from QML. You might have to add your own singleton with a method to create a key sequence from key and modifier or use the setShortcut(key, modifier) variant to set the key, as the enums are available in qml

gluax commented 3 years ago

Heya, can confirm that using the setShortcut function when exposing this to qml does work!