[英]QML several items with active focus / keyboard shortcut control
我正在嘗試為我的qml應用程序實現鍵盤快捷鍵控件。 我知道有可能使用Action元素,但我不想要必須使用的菜單和工具欄。 這就是我用鍵盤事件接近這個主題的原因。 為此,我需要讓執行操作的元素成為焦點。 但我的目標是全局快捷控制,所以理論上我需要把所有有問題的元素都集中在一起。 我在文檔中找到了FocusScope
類型,但我不確定這是否是我需要的。 這是否意味着嵌套的FocusScopes
的焦點“滑動”到最后一個不是FocusScope
元素並通過焦點手動獲取focus: true
因此只有最后一個元素保持焦點? 或者在幻燈片下方獲取焦點的所有元素都設置了activeFocus
屬性?
這是正確的方法還是我還需要別的東西?
專注於Qt Quick在我看來是一團糟。 它總是讓我困惑,我最終用forceActiveFocus()
來破解它。 我推薦新的Shortcut
類型:
Shortcut {
sequence: StandardKey.Quit
context: Qt.ApplicationShortcut
onActivated: Qt.quit()
}
使用context
屬性,您可以選擇是否要將快捷方式應用於當前窗口或整個應用程序。
捷徑旨在取代行動。 我想在將來殺死后者,因為......
- 比較實際用戶代碼: http : //cutebin.fi/prwznhkbo
- 查看BasicButton.qml周圍的“動作”相關表達式的數量
- 恕我直言,整個概念不太適合移動/嵌入式或QML
行動是一項經常被要求的功能。 現在他們擁有它,常見的問題是“如何使用不同的圖標/文本”或“如何知道觸發動作的源”。 兩者都與Action的唯一目的相矛盾,如果它們只是首先編寫更簡單的QML代碼,那么“問題”就不會存在,如示例代碼段所示。 :)
顯然,Action最有用的部分是快捷功能。 那些需要快捷方式的人並不滿意他們需要使用Action,因為“所有這些其他東西都有,我只想要一條捷徑”。
也許有不同的方法來實現這一點,但我所知道的方式如下。
我們的想法是擁有一個控制您需要處理的關鍵事件的Item
。
我將用一個例子來解釋自己。 正如您將看到的,如果我們有輸入小部件(即TextInput
),我們必須實現一種機制來將輸入返回到Item
,以便再次處理鍵盤事件。 在此示例中, Qt.Key_Escape
鍵將用於設置焦點。
import QtQuick 2.4
import QtQuick.Controls 1.3
ApplicationWindow {
id: mainwindow
title: qsTr("Hello")
width: 640
height: 480
visible: true
Item {
anchors.fill: parent
focus: true
Keys.onPressed: {
if ( (event.key === Qt.Key_Q) && (event.modifiers & Qt.ShiftModifier) ) {
rect.blue()
} else if ( (event.key === Qt.Key_W) && (event.modifiers & Qt.AltModifier) ) {
rect.red()
} else if ( (event.key === Qt.Key_E) && (event.modifiers & Qt.AltModifier) ) {
text.text = 'Key Alt+E was pressed'
}
}
Rectangle{
id: rect
width: 100
height: 100
color: "black"
function blue() {color = "blue"}
function red() {color = "red"}
}
Text {
id: text
anchors.centerIn: parent
font.pointSize: 20
}
TextInput {
id: textinput
anchors.top: text.bottom
text: "sample text"
Keys.onPressed: {
if (event.key === Qt.Key_Escape) {
console.log('Key Escape was pressed');
parent.focus = true;
}
}
}
}
}
編輯#1: @Mitch建議使用Shortcut
QML類型。 如果你可以使用它(從Qt 5.5開始可用),代碼會略有不同。 無論如何,您還需要在某些情況下將焦點設置為主應用程序,具體取決於實現的快捷方式序列。 例如,如果我們輸入文本,則Shift+Q
在此示例中不起作用。 我們需要先按Escape鍵。
import QtQuick 2.5
import QtQuick.Controls 1.3
ApplicationWindow {
id: mainwindow
title: qsTr("Hello")
width: 640
height: 480
visible: true
Shortcut {
sequence: "Shift+Q"
onActivated: rect.blue()
context: Qt.ApplicationShortcut
}
Shortcut {
sequence: "Alt+W"
onActivated: rect.red()
context: Qt.ApplicationShortcut
}
Shortcut {
sequence: "Alt+E"
onActivated: text.text = 'Key Alt+E was pressed'
context: Qt.ApplicationShortcut
}
Item {
anchors.fill: parent
Rectangle{
id: rect
width: 100
height: 100
color: "black"
function blue() {color = "blue"}
function red() {color = "red"}
}
Text {
id: text
anchors.centerIn: parent
font.pointSize: 20
}
TextInput {
id: textinput
anchors.top: text.bottom
text: "sample text"
Keys.onPressed: {
if (event.key === Qt.Key_Escape) {
console.log('Key Escape was pressed');
parent.focus = true;
}
}
}
}
}
就像米奇一樣,我發現焦點是QML的混亂,就像許多其他方面一樣。
我最終實現了自己的“主動焦點/選擇”方案。 基本上我將項目指針列表保存為我的“活動選擇”,我將鍵盤焦點固定在作為事件調度程序的單個項目上,並將鍵盤事件重定向到活動選擇列表中的所有項目。 我仍然使用QML的MouseArea
來管理所選項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.