为QML组件分配键盘快捷键

Vik*_*ava 12 qt qml qt-quick qt4.7 qt5.1

我正在使用QML和Qt Creator构建桌面应用程序,我目前正在研究键盘处理以及它如何与QML元素一起工作.我已经意识到桌面小部件缺少适当的QML替换.

我目前的问题是,我希望为一些特定的QML组件分配一些全局键盘快捷键(比如为GUI上的按钮分配键盘快捷键),这些快捷键应该激活它们.我能管理的最好的是使用FocusScopes和Key Navigation能够通过键盘导航GUI,但这不是一回事.

任何人都可以建议在这种情况下做什么?Qt 5有没有这样的功能?我在互联网上找不到任何相关信息.

Vik*_*ava 10

回答我自己的问题,现在可以在Qt 5.1.1中实现快捷方式.快捷方式可以轻松绑定到QtQuick控件之类Button,ToolButtonsMenuItem使用QML Action项.例如:

ApplicationWindow {
    ...
    ToolButton { action: openAction } // Add a tool button in a ToolBar
    ...
    Action {
        id: openAction
        text: "&Open"
        shortcut: "Ctrl+O"
        onTriggered: // Do some action
        tooltip: "Open an image"
    }
}
Run Code Online (Sandbox Code Playgroud)

按Ctrl + O将执行onTriggered部分中指定的操作.

请参阅Qt Quick Controls Gallery示例


Ken*_*Ken 5

您完全可以通过使用 C++(Qt) 中的 EventFilter 在 QML 中使用快捷方式。

您可以通过以下步骤进行:

1. Create a Shortcut class by C++.
2. Register QML Type for Shortcut class
3. Import Shortcut to QML file and handle it.
Run Code Online (Sandbox Code Playgroud)

1. Create a Shortcut class by C++.
2. Register QML Type for Shortcut class
3. Import Shortcut to QML file and handle it.
Run Code Online (Sandbox Code Playgroud)

#ifndef SHORTCUT_H
#define SHORTCUT_H

#include <QDeclarativeItem>

class Shortcut : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVariant key READ key WRITE setKey NOTIFY keyChanged)
public:
    explicit Shortcut(QObject *parent = 0);

    void setKey(QVariant key);
    QVariant key() { return m_keySequence; }

    bool eventFilter(QObject *obj, QEvent *e);

signals:
    void keyChanged();
    void activated();
    void pressedAndHold();

public slots:

private:
    QKeySequence m_keySequence;
    bool m_keypressAlreadySend;
};

#endif // SHORTCUT_H
Run Code Online (Sandbox Code Playgroud)

#include "shortcut.h"
#include <QKeyEvent>
#include <QCoreApplication>
#include <QDebug>
#include <QLineEdit>
#include <QGraphicsScene>

Shortcut::Shortcut(QObject *parent)
    : QObject(parent)
    , m_keySequence()
    , m_keypressAlreadySend(false)
{
    qApp->installEventFilter(this);
}

void Shortcut::setKey(QVariant key)
{
    QKeySequence newKey = key.value<QKeySequence>();
    if(m_keySequence != newKey) {
        m_keySequence = key.value<QKeySequence>();
        emit keyChanged();
    }
}

bool Shortcut::eventFilter(QObject *obj, QEvent *e)
{
    if(e->type() == QEvent::KeyPress && !m_keySequence.isEmpty()) {
//If you want some Key event was not filtered, add conditions to here
        if ((dynamic_cast<QGraphicsScene*>(obj)) || (obj->objectName() == "blockShortcut") || (dynamic_cast<QLineEdit*>(obj)) ){
            return QObject::eventFilter(obj, e);
        }
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(e);

        // Just mod keys is not enough for a shortcut, block them just by returning.
        if (keyEvent->key() >= Qt::Key_Shift && keyEvent->key() <= Qt::Key_Alt) {
            return QObject::eventFilter(obj, e);
        }

        int keyInt = keyEvent->modifiers() + keyEvent->key();

        if(!m_keypressAlreadySend && QKeySequence(keyInt) == m_keySequence) {
            m_keypressAlreadySend = true;
            emit activated();
        }
    }
    else if(e->type() == QEvent::KeyRelease) {
        m_keypressAlreadySend = false;
    }
    return QObject::eventFilter(obj, e);
}
Run Code Online (Sandbox Code Playgroud)

qmlRegisterType<Shortcut>("Project", 0, 1, "Shortcut");
Run Code Online (Sandbox Code Playgroud)


mar*_*tin 5

从 Qt 5.9 开始,甚至包含了所需的行为:

import QtQuick 2.9

Item {
    Shortcut {
       context: Qt.ApplicationShortcut
       sequences: [StandardKey.Close, "Ctrl+W"]

        onActivated: {
            container.clicked()
            console.log("JS: Shortcut activated.")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果省略上下文,它将仅适用于当前活动的窗口,否则适用于整个应用程序,请参阅文档