且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

QML:如何注册用户自定义快捷方式

更新时间:2023-01-31 14:01:00

QML 的美妙之处在于您可以轻松地做任何想做的事.至于您的问题,您可以执行以下操作:

TextField {编号:txt只读:真anchors.centerIn:父级Keys.onPressed:{var str = '';无功 arr = {'Shift': Qt.ShiftModifier,'Ctrl': Qt.ControlModifier,'Alt': Qt.AltModifier,'元':Qt.MetaModifier,'键盘':Qt.KeypadModifier,'GroupSwitch': Qt.GroupSwitchModifier };for(arr 中的 var 键){var 值 = arr[key];if(event.modifiers & value) {str += (str === '' ? '' : '-') + key;}}if(event.text !== '')str += (str === '' ? '' : '-') + event.text;txt.text = str;}}

I'm currently learning Qt by developing a simple desktop app with Python and QML. The document about Pyside2 with QML is surprisingly insufficient.

Anyway, now I'm struggling with user custom shortcut key registering.

Some app have a text field in which you can press a key combination and that combination will show in the text field. something like this image

According to the official document, there's a QKeySequenceEdit in C++.

Is there any way to do this in QML? Or at least in Pyside2?

Or maybe QML is not a good choice for GUI from beginning?


update

strange characters using @folibis's answer

Ctrl+d`, Ctrl+c, Ctrl+v left to right.


update 2 I ended up with code below based on @folibis's answer:


var keyModifiers = [
    {"name": "Ctrl", "code": Qt.ControlModifier},
    {"name": "Shift", "code": Qt.ShiftModifier},
    {"name": "Alt", "code": Qt.AltModifier},
    {"name": "Meta", "code": Qt.MetaModifier},
    // {"name": "Keypad", "code": Qt.KeypadModifier},
    {"name": "GroupSwitch", "code": Qt.GroupSwitchModifier}    
]
var numpadKeycodes = [
    {"name": "Del", "code": Qt.Key_Period},
    {"name": "+", "code": Qt.Key_Plus},
    {"name": "*", "code": Qt.Key_Asterisk},
    {"name": "-", "code": Qt.Key_Minus},
    {"name": "/", "code": Qt.Key_Slash},
    {"name": "0", "code": Qt.Key_0},
    {"name": "1", "code": Qt.Key_1},
    ...   
]
var keycodes = [
    ...
    {"name": "/", "code": Qt.Key_Question}, // ?
    {"name": "2", "code": Qt.Key_At}, // @
    {"name": "A", "code": Qt.Key_A},
    {"name": "B", "code": Qt.Key_B},
    {"name": "C", "code": Qt.Key_C},
    ....
]

TextField {
    id: txt
    readOnly: true

    Keys.onPressed: {
        var str = "";
        var text;
        keyModifiers.forEach(modifier => {
            if(event.modifiers & modifier.code) {
                str += `${modifier.name} + `;
            }
        });
        // Numpad keys
        if (event.modifiers & Qt.KeypadModifier) {
            str += "Num "
            text = numpadKeycodes.some(keycode => {
                if (keycode.code === event.key) {
                    str += keycode.name;
                    return keycode.name
                }
            });
        } else {
            text = keycodes.some(keycode => {
                if (keycode.code === event.key) {
                    str += keycode.name;
                    return keycode.name
                }
            });
        }
        if (str === "" && !text) {
            str = "None"
        }
        txt.text = str;
    }
    Keys.onReleased: {
        if (txt.text.endsWith(" ")) {
            txt.text = "None"
        }
    }
}

Job's done but too much code... If there's a better way, please let me know.

The beauty of QML is that you can easy do whatever you want. As for your issue you can implement something like the following:

TextField {
    id: txt
    readOnly: true
    anchors.centerIn: parent
    Keys.onPressed: {
        var str = '';
        var arr = {
            'Shift': Qt.ShiftModifier,
            'Ctrl': Qt.ControlModifier,
            'Alt': Qt.AltModifier,
            'Meta': Qt.MetaModifier,
            'Keypad': Qt.KeypadModifier,
            'GroupSwitch': Qt.GroupSwitchModifier };

        for(var key in arr) {
            var value = arr[key];
            if(event.modifiers & value) {
                str += (str === '' ? '' : '-') + key;
            }
        }
        if(event.text !== '')
            str += (str === '' ? '' : '-') + event.text;
        txt.text = str;
    }
}