2015-05-29 95 views
2

我的QML有问题。我想根据某个操作编辑TextInput,并将focus属性设置为true。它适用于TextInput位于Rectangle,但不在ScrollView中。 下面是一个例子:在滚动视图中编辑TextInput

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

也许组件的树是滚动型不可用。

但是如果你使用:的

ti1.forceActiveFocus(); 

代替:

ti1.focus = true 

它的工作原理。

相关问题