Keyboard QML Can';捕获键盘事件

Keyboard QML Can';捕获键盘事件,keyboard,focus,qml,listviewitem,Keyboard,Focus,Qml,Listviewitem,我有一个页面,其中显示了三个项目。当第一次打开页面时,我显示项目#1(其他两个可见=false)。我可以在items Keys.onPressed事件中捕捉键盘事件。然后在一些特定的选择中,我显示项目2并隐藏项目1、项目3。(即项目#2包含动态加载的ListView)。我可以捕获此项中的键盘事件以及我的Keys.onPressed事件。根据一些用户选择,我显示项目#3,隐藏项目#2,项目#1(同样使用visible=false)。项目#3正在正确显示,但我无法捕获此项目的Keys.onPress

我有一个页面,其中显示了三个项目。当第一次打开页面时,我显示项目#1(其他两个可见=false)。我可以在items Keys.onPressed事件中捕捉键盘事件。然后在一些特定的选择中,我显示项目2并隐藏项目1、项目3。(即项目#2包含动态加载的ListView)。我可以捕获此项中的键盘事件以及我的Keys.onPressed事件。根据一些用户选择,我显示项目#3,隐藏项目#2,项目#1(同样使用visible=false)。项目#3正在正确显示,但我无法捕获此项目的Keys.onPressed事件,尽管我已获得焦点(我已使用QML调试进行了测试)。我甚至无法捕捉到此页面上任何项目的按键.on(显示或未显示-甚至在显示项目#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;
    }
}
导入QtQuick 1.1
项目{
id:wifilist_屏幕
宽度:1920;
身高:1080;
联系{
目标:guiStrings;
onApsPageReadyToOpen:{
hideEnableDisableScreen();
showApsListScreen();
}
}
联系{
目标:guiStrings;
ONOPENWIFI身份验证页面:{
HideAsListScreen();
showWifiAuthenticationPage();
}
}
长方形{
id:列表屏幕;
宽度:parent.width;
高度:parent.height;
颜色:“黑色”;
//===================无线认证屏幕
//此项显示有以下两个功能。
//HideAppSlistScreen,showWifiAuthenticationPage。问题是
//显示此页面时,我无法从键盘输入。我有
//QML调试和wifiAuthScreen是否具有activeFocus=true
//和authTypeOption.focus设置为true,但当我从
//键盘我希望它能被按键捕捉到
//我没有。即使我没有通过任何按键捕捉按键事件。ON按下
//本页甚至上一页中提到的处理程序。
//只有在列表视图后打开此屏幕时,我才会看到此问题
//屏幕(即包含动态列表模型的屏幕)。
项目{
id:wifiAuthScreen;
可见:假;
焦点:假;
//显示功能菜单选项
菜单选择{
id:authTypeOption;
y:收割台高度+50;
x:200;
text:guiStrings.txtSecurity;
expandableOption:true;
选择:正确;
内在:真实;
选择:guiStrings.txtNone;
选择被禁用:真;
Keys.on按下:handleKeyEvent(事件);
}
}
//================启用/禁用屏幕
//注:这是在以下情况下页面上显示的第一项:
//我们打开页面,它显示了一个包含两个选项的菜单
启用/禁用。按下启用将导致C++
//用于在PSPageReadyToOpen()上抛出信号的代码
//然后我捕捉到这个信号并调用函数
//hideEnableDisableScreen:我隐藏此项目
//ShowAPListScreen:我显示以下项目。
项目{
id:启用禁用屏幕;
可见:真实;
焦点:正确;
菜单选择{
id:启用选项;
y:收割台高度;
文本:guiStrings.txtOn;
可扩展选项:false;
选择:“;
选择:正确;
}

Keys.onPressed:handleKeyEvent(事件);首先注意一点:在QML1中,焦点已断开(但在QMl2中为ok),请小心。