2016-09-13 110 views
1

我有一个ListView有很多行,每行有几个Items,我想通过上下键选择一行,然后选择按空格突出显示的行或输入密钥,并从第一个按钮上下导航其项目钥匙,怎么可能?如何仅浏览ListView当前索引的项目?

这里是我的代码:

import QtQuick 2.6 
import QtQuick.Controls 1.5 
import QtQuick.Controls.Styles 1.4 
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" 
      } 
     } 
     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 
         } 
        } 
        Button 
        { 
         text: model.text1 
         height: parent.height 

         onFocusChanged: 
         { 
          if(focus) 
           text="selected" 
          else 
           text= model.text3 
         } 
        } 
        Button 
        { 
         text: model.text1 
         height: parent.height 

         onFocusChanged: 
         { 
          if(focus) 
           text="selected" 
          else 
           text= model.text3 
         } 
        } 
       } 
       onFocusChanged: 
       { 
        if(focus) 
         delegateItem.color="red" 
        else 
         delegateItem.color="blue" 
       } 
      } 
      Keys.onDownPressed: { 
       if (list.currentIndex + 1 < list.count) 
        list.currentIndex += 1; 
      } 
      Keys.onUpPressed: { 
       if (list.currentIndex >= 0) 
        list.currentIndex -= 1; 
      } 
      Keys.onEnterPressed: 
      { 
       list.currentItem.forceActiveFocus() 
      } 
     } 
    } 
} 
+0

你想如何选择行?用鼠标?通过点击其中一个按钮或按钮外部? – Mitch

+0

通过键盘,我想选择第一个选定行的按钮, –

回答

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 
      } 
     } 
    } 
} 
0

使用你的榜样,你可以用箭头键导航选择行,然后按标签焦点赋予一个单独的按钮,直到你到达一个你对空间感兴趣。一旦空间聚焦,空间就会激活按钮。


你的答案与你在你的第三编辑添加的信息相比,更清洁的方式将是给每一个按钮上id并参阅。也就是说,而不是这样做:

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

这是有点脆弱,难以阅读,这样做:

KeyNavigation.down: button2 
+0

不,我想从选定行上的第一个按钮开始 –

+0

也许你可能想指定在你的问题之前downvoting答案是正确的给出你的信息提供? – Mitch

+0

我不明白你说的是什么!!! –

相关问题