2012-08-30 112 views
8

我深入到建设一个桌面应用程序与QML和Qt Creator和我目前正在研究的键盘操作以及它如何与QML元素的作品。我已经意识到缺少适用于桌面小部件的QML替代品。分配键盘快捷键QML组件

我现在的问题是,我希望分配一些全局键盘快捷键来一些特别的QML组件(如分配键盘快捷键来在GUI上的按钮),这应该激活它们。我可以管理最好是用FocusScopes和键导航,以便能够通过键盘只是导航界面,但这是不一样的东西。

任何人都可以表明,在这种情况下怎么办? Qt 5有没有这样的功能?我无法在互联网上找到关于此的任何信息。

+0

试试这个QShortcut http://doc.qt.nokia.com/4.7-snapshot/qshortcut.html – RajaRaviVarma

+1

QShortCut适用于基于QWidget的类。没有直接的方法可以使本地QML元素响应全球捷径。例如,可以将键分配给QML按钮,但仅当按钮具有焦点时才起作用。 –

+2

[在QML中使用QShortcut的应用程序范围快捷方式](http://kdeblog.mageprojects/2012/2012/11/28/application-wide-shortcuts-using-qshortcut-in-qml/)在同一行中是有趣的。我正在使用QDeclarativeView(基于QWidget)作为主内嵌QML的GUI屏幕,因此现在很容易实现应用程序范围内的快捷方式。 –

回答

8

回答我的问题作为快捷键现在可以使用Qt 5.1.1来实现。 快捷方式可以很容易地结合到QtQuick控制像ButtonToolButtonsMenuItem使用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" 
    } 
} 

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

在C++(QT)使用EventFilter参考Qt Quick Controls Gallery example

0

所以假设你正在呼吁像这样的按钮点击事件的函数,

Button { 
    ... 
    MouseArea { 
    anchor.fill: parent 
    onClicked: callThisFunction(); 
    } 
} 

然后您可以分配以这种方式分配全局键盘快捷键。但是限制是全局QML元素(包含所有其他QML元素的父元素)应该有重点。防爆。 :

Rectangle { 
    id: parentWindow 
    ... 
    ... 
    Button { 
    ... 
    MouseArea { 
     anchor.fill: parent 
     onClicked: callThisFunction(); 
    } 
    } 
    Keys.onSelectPressed: callThisFunction() 
} 

这不完全是你想要的,但它可能有所帮助。

+0

请注意简单介绍反对的原因。 – RajaRaviVarma

4

你完全可以使用快捷键在QML。

您可以在下面的步骤做的:

1. Create a Shortcut class by C++. 
2. Register QML Type for Shortcut class 
3. Import Shortcut to QML file and handle it. 

#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

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

qmlRegisterType<Shortcut>("Project", 0, 1, "Shortcut");

import Project 0.1 
 

 
Rectangle { 
 
................. 
 
................. 
 
Shortcut { 
 
     key: "Ctrl+C" 
 
     onActivated: { 
 
      container.clicked() 
 
      console.log("JS: " + key + " pressed.") 
 
     } 
 
    } 
 

 
}

+0

Thx,它确实有效 – Zeks

0

从Qt的5.9所需的行为开始甚至included

import QtQuick 2.9 

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

     onActivated: { 
      container.clicked() 
      console.log("JS: Shortcut activated.") 
     } 
    } 
} 

如果省略的背景下,它只会为当前活动窗口的工作,否则对于整个应用程序,请参见documentation