2017-10-23 4 views
0

QML로 테스트 시스템을 구현하고 있습니다. 섹션 (예 : 질문)에서 일부 대답을 누른 후 answeredtrue에서 변경되어야하므로 더 이상이 섹션에서 답을 누를 수 없습니다. 나는 answerDelegateMouseArea 클릭 핸들러에서 확인합니다. 하지만 answered 속성에 액세스 할 수 없습니다.다른 구성 요소의 구성 요소 속성 가져 오기

어떻게 연결하나요?

Rectangle { 
    id: root 
    anchors.fill: parent 

    property ListModel testModel: ListModel {} 
    property int totalAnswers: 0 
    property int questionsCount: 0 

    function setTestModel(tests){ 
     testModel.clear() 
     testModel.append(tests) 
    } 

    ScrollView { 
     id: testsScroll 
     anchors.fill: parent 

     ListView { 
      id: testsView 
      anchors.fill: parent 
      model: testModel 

      delegate: answersDelegate 

      section.property: "question" 
      section.delegate: questionDelegate 
     } 
    } 

    Component { 
     id: questionDelegate 

     Rectangle{ 
      id: questionItem 
      // the property I need to get access 
      property bool answered: false 
      width: parent.width 
      height: 40 

      Text { 
       anchors.left: parent.left 
       text: section 
      } 

      Component.onCompleted: { 
       questionsCount += 1 
      } 
     } 
    } 

    Component { 
     id: answersDelegate 
     Item { 
      id: oneItem 
      width: parent.width 
      height: 30 

      Rectangle { 
       id: answerRectangle 

       Text { 
        anchors.left: parent.left 
        text: content 
       } 

       MouseArea { 
        anchors.fill: parent 
        onClicked: { 
         // here I check it 
         root.answerClicked() 
         if (!questionDelegate.answered) { 
          questionDelegate.answered = true 
          answerRectangle.color = correct == true ? "#00ff00": "#ff0000" 
          if (correct) total += 1 
         } 
        } 
       } 
      } 
     } 
    } 
} 

답변

2

당신은 구성 요소 속성에 액세스 할 수 없습니다. Component은 인스턴스가 아닙니다. 동일한 구성으로 여러 개의 인스턴스를 생성하도록 설정된 공장과 같습니다.

ListView의 대리인으로 Component이있는 경우보기 포트에 맞을 수있는 한 모델의 항목 당 하나씩 여러 인스턴스가 만들어집니다. 이제 컴포넌트의 인스턴스가 두 개 이상 있고 각 인스턴스마다 속성 값이 다를 수 있습니다. 어느 것을 읽을 것으로 예상됩니까?

따라서 id의와 Components을 사용하여 내부 정보에 액세스 할 수 없습니다. 대신 어딘가에서 만든 구체적인 인스턴스에 액세스하고이 인스턴스의 속성을 읽어야합니다.

대리인이 동적으로 만들어지고 소멸되므로 대리인의 상태를 설정하는 것은 바람직하지 않습니다. 위임자 (질문 또는 answerDelegate)에 answered -flag를 설정 한 다음 스크롤하면 응답이보기 포트에서 나가고 대리인이 삭제되어 정보가 손실됩니다.

대신 모델에서 변경 사항을 더 영구적으로 되돌릴 수 있습니다.

관련 문제