2016-08-05 92 views
1

我想安排一个特定的控件来获得焦点在TabViewTab内。无论我做什么,似乎都是第一个获得焦点。我曾尝试在其他地方设置focus:false,但它不起作用。QT 5.7 QML如何控制哪个控件获得TabView中的焦点

请考虑以下代码。我有一个简单的列,其中包含两个RadioButton和一个TextField。我想为TextField安排被选中的标签时,总是焦点,但它总是转到第一个RadioButton

import QtQuick 2.7 
import QtQuick.Controls 1.4 
import QtQuick.Controls.Styles 1.4 
import QtQuick.Layouts 1.3 

ApplicationWindow 
{ 
    visible: true 
    width: 800 
    height: 400 

    TabView 
    { 
     anchors.fill: parent 

     Tab { title: "tab1"; sourceComponent: foo } 
     Tab { title: "tab2"; sourceComponent: foo } 
    } 

    Component 
    { 
     id: foo 
     ColumnLayout 
     { 
      spacing: 32 
      ExclusiveGroup { id: optionGroup } 

      RadioButton 
      { 
       // i always get the focus!! 
       exclusiveGroup: optionGroup 
       text: "Click me" 
       activeFocusOnPress: true 
       focus: false 
      } 

      RadioButton 
      { 
       exclusiveGroup: optionGroup 
       text: "No, click me!" 
       activeFocusOnPress: true 
       focus: false 
      } 

      TextField 
      { 
       // but i want the focus 
       placeholderText: "type here" 
       focus: true 
      } 
     } 
    } 
} 

按“TAB2”看到这一点,

enter image description here

我试图通过增加内TabView强迫,

onCurrentIndexChanged: getTab(currentIndex).item.forceActiveFocus()

但它没有区别。

我读过重点的解释,但在这种情况下没有帮助。

感谢您的任何建议或帮助,

回答

0

可能是一个错误。尝试Qt Quick Controls 2.0代替:

import QtQuick 2.7 
import QtQuick.Controls 2.0 
import QtQuick.Layouts 1.3 

ApplicationWindow { 
    visible: true 
    width: 800 
    height: 400 

    header: TabBar { 
     id: bar 
     width: parent.width 
     TabButton { 
      text: qsTr("tab1") 
     } 
     TabButton { 
      text: qsTr("tab2") 
     } 
    } 

    StackLayout { 
     anchors.fill: parent 
     currentIndex: bar.currentIndex 

     ColumnLayout { 
      id: columnLayout 
      spacing: 32 

      ButtonGroup { 
       id: optionGroup 
       buttons: columnLayout.children 
      } 

      RadioButton { 
       text: "Click me" 
      } 

      RadioButton { 
       text: "No, click me!" 
      } 

      TextField { 
       placeholderText: "type here" 
       focus: true 
      } 
     } 
    } 
} 
+0

我希望你不会说:-)一个从身体的控制1码,我很不愿意改变因素这个问题。然而,我会尝试你的回答,看看我是否可以混合使用一些控件2来修复它。谢谢。 –

+0

这就是答案。你是对的,它优于使用'StackLayout'我从来不喜欢'TabView',因为'Tab's是加载器。我在StackLayout中使用Controls1,它工作正常。谢谢。 –