如何仅导航ListView的当前索引的项目?

时间:2016-09-13 11:51:26

标签: qt qml qtquick2

我有一个<object>有很多行,每行都有很少var birthDate = prompt("write your birthdate!"); birthDateSplitter = birthDate.split(""); len = birthDateSplitter.length, birthName = 0; var i = 0; for (i; i < len; i++) { if (birthDateSplitter[i] == 9) { birthName = "do"; } else if (birthDateSplitter[i] == 8) { birthName = "ka"; } else if (birthDateSplitter[i] == 7) { birthName = "mu"; } else if (birthDateSplitter[i] == 6) { birthName = "ssa"; } else if (birthDateSplitter[i] == 5) { birthName = "tto"; } else if (birthDateSplitter[i] == 4) { birthName = "ti"; } else if (birthDateSplitter[i] == 3) { birthName = "le"; } else if (birthDateSplitter[i] == 2) { birthName = "pa"; } else if (birthDateSplitter[i] == 1) { birthName = "bu"; } else { birthName = "vi"; } birthName += birthName; } alert("Your real birthname is " + birthName); ,我想通过上下键选择一行,然后按空格选择突出显示或输入密钥并导航它的项目来自第一个按钮上下键,怎么可能?

这是我的代码:

ListView

2 个答案:

答案 0 :(得分:1)

使用您的示例,您可以通过使用箭头键导航来选择行,并通过按 tab 将焦点放在单个按钮上,直到找到您感兴趣的那个。有焦点后激活按钮。

将您的答案与您在第三次编辑中添加的信息进行比较,更简洁的方法是给每个按钮powershell.exe -ExecutionPolicy Unrestricted -File C:\Pushover-DelugeNotify.ps1 "Test" 并参考。也就是说,而不是这样做:

id

有点脆弱且难以阅读,请执行此操作:

KeyNavigation.down: list.currentItem.children[0].children[1]

答案 1 :(得分:0)

要在所选行的第一个按钮上导航,您应该使用list.currentItem.children这里是修改后的代码

import QtQuick 2.6
import QtQuick.Controls 1.5

ApplicationWindow {
    visible: true
    width: 400
    height: 480
    title: qsTr("Hello World")

    Rectangle {
        anchors.fill: parent
        ListModel {
            id: myModel
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
            ListElement {
                text1: "btn1"
                text2: "btn2"
                text3: "btn3"
            }
        }
        ListView {
            id: list
            anchors.fill: parent
            model: myModel
            currentIndex: 0
            focus: true
            delegate: Rectangle {
                id: delegateItem
                height: 100
                width: parent.width
                color: "blue"

                Row {
                    anchors.fill: parent
                    Button {
                        text: model.text1
                        height: parent.height

                        onFocusChanged: {
                            if (focus)
                                text = "selected"
                            else
                                text = model.text1
                        }
                        KeyNavigation.down: list.currentItem.children[0].children[1]
                    }
                    Button {
                        text: model.text1
                        height: parent.height

                        onFocusChanged: {
                            if (focus)
                                text = "selected"
                            else
                                text = model.text3
                        }
                        KeyNavigation.down: list.currentItem.children[0].children[2]
                         KeyNavigation.up: list.currentItem.children[0].children[0]
                    }
                    Button {
                        text: model.text1
                        height: parent.height

                        onFocusChanged: {
                            if (focus)
                                text = "selected"
                            else
                                text = model.text3
                        }
                         KeyNavigation.up: list.currentItem.children[0].children[1]
                    }
                }
                onFocusChanged: {
                    if (focus)
                        delegateItem.color = "red"
                    else
                        delegateItem.color = "blue"
                    console.log(list.currentItem.children[0].children[0].focus)
                }
                Keys.onDownPressed:
                {
                        if (list.currentIndex + 1 < list.count)
                            list.currentIndex += 1
                }
                Keys.onUpPressed:
                {
                        if (list.currentIndex -1 > -1)
                            list.currentIndex -= 1

                }


                Keys.onSpacePressed: {
                    var focus=list.currentItem.children[0].children[children_index].focus
                    list.currentItem.children[0].children[children_index].focus
                            = !focus
                    if(focus)
                        list.currentItem.focus=true
                    console.log("entered: "+list.currentItem.children[0].children[0].focus)
                }
                property int children_index: 0
            }
        }
    }
}