2014-12-02 6 views
4

qt 빠른 응용 프로그램에서 작업 중이므로 대화 상자를 열고 싶습니다. 이 대화 상자 창에서 TextField가 있는데 대화 상자가 열린 후에이 텍스트로 포커스를 설정하고 싶습니다. 이 코드는 작동하지 않습니다. 콘솔에QML 대화 상자에 집중된 textField가 있습니다.

function newFolder() { 
    newFolderDialog.visible = true 
    newFolderDialog.open() 
} 

Dialog { 
    id: newFolderDialog 
    title: "New folder" 
    height: 150 
    width: 300 
    standardButtons: StandardButton.Ok | StandardButton.Cancel 

    Column { 
     anchors.fill: parent 
     Text { 
      text: "Name" 
      height: 40 
     } 
     TextField { 
      id: newFolderInput 
      width: parent.width * 0.75 
      focus: true 
      onFocusChanged: console.log("Focus changed " + focus) 
     } 
    } 

    onVisibilityChanged: { 
     if(visible === true){ 
      newFolderInput.text = "" 
      newFolderInput.focus = true 
     } 

    } 
} 

출력은

QML입니다 : 초점
거짓 QML을 변경 : 초점
사실 QML을 변경 : 초점이 변경 거짓 어떻게 든 집중

그것은처럼, textField에 포커스를 설정 한 후 변경되었습니다.

+0

이 기능은 Qt 5.5에서 유용합니다. 포커스는 TextField로 이동하고 콘솔에는 네 줄 (false, true, false, true)이 있습니다. – pepan

답변

5

그것이 쓰여졌을 때 함수를 eed했다. 기능 open()에 대한 Dialog의 문서에서 :

는 사용자에게 대화 상자를 표시합니다.을 true로 설정하는 것과 동일한 것은 입니다.

문제가 아니므로 대화와 포함 된 요소간에 지속적으로 초점이 맞춰져있는 것처럼 보입니다. Dialog을 열거 나 닫을수록 평가가 더 많이 발생합니다. 나는 왜 이런 일이 일어나는 지 알 수 없다. 그러나 (1) onVisibilityChanged 처리기를 제거하고 (2) newFolder()을 다시 작성하여 쉽게 문제를 해결할 수 있습니다. 최종 코드를 다시 쓴 : 당신이 먼저 대화 상자를 열고

ApplicationWindow { 
    width: 360 
    height: 300 
    visible: true 

    Button { 
     anchors.centerIn: parent 
     text: "click me!" 
     onClicked: newFolder() 
    } 

    Dialog { 
     id: newFolderDialog 
     title: "New folder" 
     height: 150 
     width: 300 
     standardButtons: StandardButton.Ok | StandardButton.Cancel 
     focus: true // Needed in 5.9+ or this code is NOT going to work!! 

     Column { 
      anchors.fill: parent 
      Text { 
       text: "Name" 
       height: 40 
      } 
      TextField { 
       id: newFolderInput 
       width: parent.width * 0.75 
       focus: true 
       onFocusChanged: console.log("Focus changed " + focus) 
      } 
     } 
    } 

    function newFolder() { 
     newFolderDialog.open() 
     newFolderInput.focus = true 
    } 
} 

이 방법은 적절한 Item에 포커스를 설정합니다.

+0

해결책이 도움이되지 않았습니다. 대화 상자를 올바르게 표시하지만 textField에 포커스를 추가하지 않습니다. – user3412372

+0

5.3 및 5.4에서 저에게 적합합니다. 전체 테스트 코드 (코드 + 버튼)를 편집했습니다. 어쩌면 누락 된 설정에 대해 뭔가가있을 수 있습니까? – BaCaRoZzo

+1

예, 작동합니다! 내 잘못했다.'newFolderInput.focus = true' 대신에'newFolderInput.visible = true'를 썼다. 정말 고마워! – user3412372