QML:如何注册用户自定义快捷方式 [英] QML: how to register user custom shortcut

查看:79
本文介绍了QML:如何注册用户自定义快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过使用 Python 和 QML 开发一个简单的桌面应用程序来学习 Qt.关于Pyside2QML 的文档出奇地不够.

无论如何,现在我正在为用户自定义快捷键注册而苦苦挣扎.

某些应用程序有一个文本字段,您可以在其中按下组合键,该组合将显示在文本字段中.像这张图片

根据官方文档,有一个QKeySequenceEdit 在 C++ 中.

有没有办法在 QML 中做到这一点?或者至少在 Pyside2 中?

或者 QML 从一开始就不是 GUI 的好选择?

<小时>

更新

使用@folibis 回答的奇怪字符

Ctrl+d`、Ctrl+cCtrl+v 从左到右.

<小时>

更新 2根据@folibis 的回答,我最终得到了以下代码:

<预><代码>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 键码 = [...{"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},....]文本域 {编号:txt只读:真Keys.onPressed:{var str = "";var 文本;keyModifiers.forEach(modifier => {if(event.modifiers & modifier.code) {str += `${modifier.name} + `;}});//数字键if (event.modifiers & Qt.KeypadModifier) {str += "数字"文本 = numpadKeycodes.some(keycode => {if (keycode.code === event.key) {str += keycode.name;返回keycode.name}});} 别的 {text = keycodes.some(keycode => {if (keycode.code === event.key) {str += keycode.name;返回keycode.name}});}if (str === "" && !text) {str = "无"}txt.text = str;}Keys.onReleased:{如果(txt.text.endsWith(")){txt.text = "无"}}}

工作已完成,但代码太多...如果有更好的方法,请告诉我.

解决方案

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;
    }
}

这篇关于QML:如何注册用户自定义快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆