2015-05-29 2 views
2

У меня проблема с моим QML. Я хотел бы отредактировать TextInput на основе действия, установив для атрибута focus значение true. Он работает, когда TextInput находится в Rectangle, но не в ScrollView. Вот пример:Редактирование текстового ввода в ScrollView

Item { 
    id: main 
    width: 640 
    height: 480 

    ScrollView{ 
     id: scrollView 
     height: parent.height/2 
     width: parent.width 

     Rectangle{ 
      border.color: "black" 
      border.width: 1 
      anchors.centerIn: parent 
      height: 25 
      width: 200 
      TextInput{ 
       id: ti1 
       anchors.fill: parent 
       verticalAlignment: TextInput.AlignVCenter 
       horizontalAlignment: TextInput.AlignHCenter 
      } 
     } 

    } 

    Rectangle{ 
     y: height 
     height: parent.height/2 
     width: parent.width 

     Rectangle{ 
      border.color: "black" 
      border.width: 1 
      anchors.centerIn: parent 
      height: 25 
      width: 200 
      TextInput{ 
       id: ti2 
       anchors.fill: parent 
       verticalAlignment: TextInput.AlignVCenter 
       horizontalAlignment: TextInput.AlignHCenter 
      } 
     } 
    } 

    MouseArea{ 
     anchors.fill: parent 
     onClicked: { 
      if (mouseY < parent.height/2){ 
       ti2.focus = false 
       ti1.focus = true 
      }else{ 
       ti1.focus = false 
       ti2.focus = true 
      } 
     } 
    } 

} 

Когда я нажимаю на нижней части окна, то TextInput ti2 редактируется. Но когда я нажимаю на верхнюю половину, ti1 нет.

Есть ли у кого-нибудь идеи? Поведение такое же, как и у TextEdit.

Спасибо.

ответ

2

Я думаю, это потому, что: «Только один элемент может быть прямым дочерним элементом ScrollView, а дочерний элемент неявно закреплен для заполнения прокрутки»..

От: http://doc.qt.io/qt-5/qml-qtquick-controls-scrollview.html

Возможно, дерево компонентов недоступен в ScrollView.

Но если вы используете:

ti1.forceActiveFocus(); 

вместо:

ti1.focus = true 

он работает.

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