• 3597阅读
  • 2回复

Tab页内第一个控件在Tab点击后得到了focus该怎么处理? [复制链接]

上一主题 下一主题
离线彩阳
 

只看楼主 倒序阅读 楼主  发表于: 2014-09-28
为了详细阐述我遇到的问题,我制作了一个小程序。
我在运行程序的时候,发现Tab页内的第一个控件在Tab点击了的之后得到了focus,事实上我是不希望获得focus的,这个问题该如何处理呢?
程序代码:

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("First control focus problem")

    menuBar: MenuBar {
        Menu {
            title: qsTr("File")
            MenuItem {
                text: qsTr("&Open")
                onTriggered: console.log("Open action triggered");
            }
            MenuItem {
                text: qsTr("Exit")
                onTriggered: Qt.quit();
            }
        }
    }

    TabView
    {
        id: tabView
        anchors.fill: parent
        Tab
        {
            title: qsTr( "first tab" )

            Rectangle
            {
                TextField
                {
                    id: textField
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.bottom: defaultText.top
                    text: qsTr( "textField_1" )
                }

                Text
                {
                    id: defaultText
                    text: qsTr( "It is not expected to be visible." )
                    visible: textField.focus
                    color: "red"
                    anchors.centerIn: parent
                }

                Button
                {
                    anchors.top: defaultText.bottom
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr( "Test button" )
                }
            }
        }

        Tab
        {
            title: qsTr( "first tab" )

            Rectangle
            {
                TextField
                {
                    id: textField_2
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.bottom: defaultText_2.top
                    text: qsTr( "textField_2" )
                }

                Text
                {
                    id: defaultText_2
                    text: qsTr( "It is not expected to be visible." )
                    visible: textField_2.focus
                    color: "red"
                    anchors.centerIn: parent
                }

                Button
                {
                    anchors.top: defaultText_2.bottom
                    anchors.horizontalCenter: parent.horizontalCenter
                    text: qsTr( "Test button" )
                }
            }
        }
    }
}
程序截图:

问题工程代码 FirstControlFocus.7z (2 K) 下载次数:0
红色的字不应该出现的。这是因为textField_2获得了focus。怎样才能让其不获取到focus呢?

上海Qt开发联盟,热忱地欢迎你的加入!
离线彩阳

只看该作者 1楼 发表于: 2014-09-28
找到原因了,原来是需要设置控件的activeFocusOnTab这个属性为false才行。
上海Qt开发联盟,热忱地欢迎你的加入!
离线robertkun

只看该作者 2楼 发表于: 2015-01-02
支持!n神马都是浮云
                                              简单的生活使人快乐!
快速回复
限100 字节
 
上一个 下一个