2016-09-13 2 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

от Keyboard, я хочу выбрать первую кнопку в выбранной строке, –

ответ

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

Возможно, вам может потребоваться указать, что в вашем вопросе перед тем, как ответить вниз, правильные данные, данные предоставлена? – Mitch

+0

Я не понимаю, что ты сказал !!! –

Смежные вопросы