2017-11-20 34 views
0

저는 C++에서 생성 된 복잡한 데이터 모델을 표시하기 위해 gridview를 사용하고 있습니다. gridview가 모든 keyNavigation 이벤트를 받아들이지 않거나 응답하지 않는다는 점을 제외하면 잘 작동합니다. 여기 내 코드가QML : Gridview 키보드 탐색이 작동하지 않습니다.

GridView{ 
    id: gridView 
    anchors.fill: parent 
    clip: true 
    cellWidth: 315 
    cellHeight: 300/6+15 
    focus: true 
    model: system.DataList 
    highlightFollowsCurrentItem: true 
    keyNavigationEnabled: true//enabled but still doesnt work 
    //keyNavigationWraps: true//does this matter 

    highlight: Rectangle{ 
     color: highlightColor 
     radius: 5 
     width: gridView.cellWidth 
     height: gridView.cellHeight 
     x: gridView.currentItem.x 
     y: gridView.currentItem.y 
     Behavior on x { SpringAnimation { spring: 3; damping: 0.2 } } 
     Behavior on y { SpringAnimation { spring: 3; damping: 0.2 } } 
    } 

    delegate: Component{ 
     Rectangle{ 
      id: viewParentRect 
      width: gridView.cellWidth; 
      height: diskListView.cellHeight - 15 
      color: "transparent" 

      Row{ 
       anchors.fill: parent 
       Rectangle{ 

        width: parent.width/6 ; height: parent.height 
        color: "transparent" 
        Image { 
         id: image 
         anchors.centerIn: parent 
         source: model.modelData.IconPath 
         sourceSize.width: parent.width 
         sourceSize.height: parent.height 
        } 
       } 
       Column{ 
        Text { 
         id: displayName 
         text: model.modelData.DisplayName 
         font.family: "Sans Serif" 
         font.pointSize: viewParentRect.width/30 
        } 
        Text { 
         id: usageText 
         text: model.modelData.Usage 
         font.family: "Sans Serif" 
         font.pointSize: viewParentRect.width/30 
        } 
       } 
      } 

      MouseArea{ 
       anchors.fill: parent 
       onClicked: { 
        gridView.currentIndex = index 
       } 
       onDoubleClicked: { 
        system.enter(model.modelData.Path) 
       } 
      } 
     } 

    } 

} 

특정 지점에서 중단 점을 유지하려고 시도했는데 아무런 키 누르기 이벤트가 전혀 발생하지 않았습니다. 위의 코드는 마우스로 잘 작동하지만 데스크톱 용으로 개발하고 있으므로 키보드 탐색이 제대로 작동해야합니다 (마우스 탐색이 작동하지 않더라도 문제는 아닙니다).

답변

0

글쎄, QML은 포커스 속성이 true로 설정되어 있고 사용자 포커스가있는 한 GridView 및 ListView에 대해 keyNavigation 만 제공합니다. 초점이 나중에 만들어지면 다른 구성 요소에 의해 stollen 될 수 있으며 키 탐색이 예상대로 작동하지 않는 이유입니다.

필자의 경우, 두 개의 다른 GridView를 가진 두 개의 Component/s를 만든 다음 두 GridView에서 포커스를 훔치고있는 StackView를 부모로 사용하고 있습니다.

첫 번째 해결 방법은 StackView에 의한 keyPress를 감지하고 화살표 키 및/또는 Enter/Return 키를 GridView에 전달하는 것입니다. GridView keyNavigationEnabled 속성에 false를 설정하고 이러한 이벤트를 수동으로 처리합니다. 이 방법은 챔피언처럼 작동했지만 코드가 너무 복잡해졌습니다. 그래서 두 번째 방법을 썼습니다.

동적으로 두 구성 요소를 만들고 StackView 안에 하나만 유지하십시오. 부모 GridView (및 독점적 인 포커스가 필요한 다른 항목)를 FocusScope와 함께 사용하고 부모 구성 요소가 포커스를 탐지하여 하위로 전송합니다. 결국 QML이 모든 주요 탐색을 처리하게하십시오. 여기서 첫번째 터지는 코드,

main.qrc

StackView{ 
     id: stackWindow 
     width: mainWindow.width 
     height: mainWindow.height 
     focus: true 
     Component.onCompleted: {push(Qt.createComponent("qrc:/PrimaryHomePage.qml"))} 
     onCurrentItemChanged: {currentItem.forceActiveFocus()} 
     Connections{ 
      target: rSystem 
      ignoreUnknownSignals: true 
      onSwitchBetweenViews: { 
       if(newValue === 1){ 
        stackWindow.pop() 
        stackWindow.push(Qt.createComponent("qrc:/SecondaryViewPage.qml")) 
       } 
       else if(newValue === 0){ 
        stackWindow.pop() 
        stackWindow.push(Qt.createComponent("qrc:/PrimaryHomePage.qml")) 
       } 
      } 
     } 
    } 

는 StackView가 (뿐만 아니라 사용할 수있는 대체)는 항목에 하나의 항목을 소유하고 있음을 보장합니다.

PrimaryHomePage.qrc

Item { 
id: primaryHomePageParentItem 
width: parent.width 
height: parent.height 

FocusScope{ 
    id: focusScope 
    anchors.fill: parent 
    rGridView{ 
      id: GridView_local 
      availablewidth: parent.width 
     } 
} 
onFocusChanged: {focusScope.focus = true} 
} 

그리고 좋은 점은, 더 빨리 첫 번째 방법보다 더 작동합니다.