我有一个页面,我显示三个项目。当页面第一次打开时,我显示项目#1(另外两个是可见的=假)。我可以在Keys.onPressed事件中捕获键盘事件。然后在某些特定选择中,我显示项目#2并隐藏项目#1,项目#3。(即项目#2包含动态加载的ListView)。我可以在Keys.onPressed事件中捕获此项目中的键盘事件。基于一些用户选择,我显示项目#3,并隐藏项目#2,项目#1(再次使用visible = false)。项目#3正在正确显示,但我无法捕获此项目的Keys.onPressed事件,尽管事实上我已经获得了焦点(我已经使用QML调试进行了测试)。我甚至无法捕捉到这个页面上任何项目的Keys.onPressed(显示或未显示 - 甚至在我显示第3项之前我都没有抓住这个事件)。
仅当我在第2项之后打开第3项时,才会出现此问题,第2项中唯一特殊的事情是动态创建ListView。
以下是我的代码,我试图在这里提出足够的评论。
import QtQuick 1.1
Item {
id: wifilist_screen
width: 1920;
height: 1080;
Connections {
target: guiStrings;
onApsPageReadyToOpen: {
hideEnableDisableScreen();
showApsListScreen();
}
}
Connections {
target: guiStrings;
onOpenWifiAuthenticationPage: {
hideApsListScreen();
showWifiAuthenticationPage();
}
}
Rectangle {
id: listScreen;
width: parent.width;
height: parent.height;
color: "black";
// ============== Wifi Authentication Screen
// This item is being shown with following two functions.
// hideApsListScreen, showWifiAuthenticationPage. The problem is
// I can't input from the keyboard when this page is shown. I have
// did QML debugging and wifiAuthScreen does have activeFocus = true
// and authTypeOption.focus to true but when I press key from the
// keyboard I am expecting it to be catched by Keys.onPressed, but
// I don't. Even I am not catching key events by any Keys.onPressed
// handler mentioned on this page or even its previous page.
// I see this problem only when I open this screen after list_view
// screen (i.e The screen which contains a dynamic List model.
Item {
id: wifiAuthScreen;
visible: false;
focus: false;
// Show Function Menu Options Options
MenuOption {
id: authTypeOption;
y: header.height + 50;
x: 200;
text: guiStrings.txtSecurity;
expandableOption: true;
selected: true;
inside: true;
selection: guiStrings.txtNone;
selection_disabled: true;
Keys.onPressed: handleKeyEvent(event);
}
}
// ============== Enable / Disable Screen
// NOTE: This is the first item shown on the page when
// we open the page. It shows a menu with two options
// Enable / Disable. Pressing Enable will cause C++
// code to throw signal onApsPageReadyToOpen()
// and then I catch that signal and call the function
// hideEnableDisableScreen: I hide this item
// showApsListScreen : I show following item.
Item {
id: enableDisableScreen;
visible: true;
focus: true;
MenuOption {
id: enableOption;
y: header.height;
text: guiStrings.txtOn;
expandableOption: false;
selection: "";
selected: true;
}
Keys.onPressed: handleKeyEvent(event); <-- Working great.
}
// ========== Wifi Access Points List Screen
// This shows a dynamic List model. Once I select some index item
// from the list on pressing Return I call a C++ code and that
// throws a signal onOpenWifiAuthenticationPage().
// Here I catch onOpenWifiAuthenticationPage and hide this page,
// and show page having the id: wifiAuthScreen.
Component {
id: listDelegate
MenuOption {
id: wifiOption;
text: ssid;
expandableOption: false;
selection: "";
selected: if ( index == list_view.currentIndex ) true; else false;
Keys.onPressed: {
handleKeyEvent(event); <-- Working Great.
}
}
}
ListView {
id: list_view
width: parent.width;
height: 1080 - header.height;
y: header.height;
currentIndex: 0;
boundsBehavior: Flickable.StopAtBounds
model: wifiNetworksModel;
delegate: listDelegate;
visible: false;
}
}
function handleKeyEvent(event)
{
// --------------------------------------
// We are showing Wifi Enable / Disable screen
// --------------------------------------
if ( enableDisableScreen.visible ) {
if ( event.key == Qt.Key_Up || event.key == Qt.Key_Down ) {
if ( enableOption.selected ) {
enableOption.selected = false;
disableOption.selected = true;
} else if ( disableOption.selected ) {
disableOption.selected = false;
enableOption.selected = true;
}
} else if ( event.key == Qt.Key_Return ) {
if ( enableOption.selected ) {
guiStrings.EnableDisableWifi(true);
} else if ( disableOption.selected ) {
wifiIsDisabled(true);
}
} else if ( event.key == Qt.Key_Left ) {
wifiIsDisabled(false);
}
}
// --------------------------------------
// We are showing Wifi Access Points List screen
// --------------------------------------
else if ( list_view.visible ) {
if ( event.key == Qt.Key_Up ) {
if ( list_view.currentIndex == 0 )
list_view.currentIndex = (list_view.count);
} else if ( event.key == Qt.Key_Down ) {
if ( list_view.currentIndex == (list_view.count - 1))
list_view.currentIndex = -1;
} else if ( event.key == Qt.Key_Left ) {
event.accepted = true;
hideApsListScreen();
showEnableDisableScreen();
} else if ( event.key == Qt.Key_Return ) {
event.accepted = true;
hideApsListScreen();
guiStrings.ActivateWifiConnection(list_view.currentIndex);
}
}
// --------------------------------------
// We are showing wifi authentication screen
// --------------------------------------
else if ( wifiAuthScreen.visible ) {
}
}
function showEnableDisableScreen() {
enableDisableScreen.visible = true;
enableDisableScreen.focus = true;
enableOption.selected = true;
disableOption.selected = false;
}
function hideEnableDisableScreen() {
enableDisableScreen.visible = false;
enableDisableScreen.focus = false;
}
function showApsListScreen() {
list_view.focus = true;
list_view.visible = true;
list_view.currentIndex = 0;
}
function hideApsListScreen() {
list_view.focus = false;
list_view.visible = false;
}
function showWifiAuthenticationPage() {
wifiAuthScreen.forceActiveFocus();
wifiAuthScreen.focus = true;
wifiAuthScreen.visible = true;
authTypeOption.focus = true;
currentAuthType = "none";
}
function hideWifiAuthenticationPage() {
wifiAuthScreen.focus = false;
wifiAuthScreen.visible = false;
}
}
总之,看起来键盘事件似乎在某处丢失了。我有效地专注于我想要捕获事件的项目,我甚至无法捕获应用程序主页面上的事件。如果我在包含带动态模型的ListView的项目之后打开有问题的项目,则只会出现此问题。
感谢任何帮助。
最诚挚的问候, Farrukh Arshad。