qml focus相关

摘要:
“红色”:“透明”}//摘要:activeFocus是真正的焦点

Item的属性:

read-only activeFocus : bool
This property indicates whether the item has active focus.
An item with active focus will receive keyboard input, or is a FocusScope ancestor of the item that will receive keyboard input.
Usually, activeFocus is gainedby setting focus on an item and its enclosing FocusScopes.(两个都要focus) In the following example input will have activeFocus.


Rectangle {
     FocusScope {
         focus: true
         TextInput {
             id: input
             focus: true
         }
     }
}

focus : bool
This property indicates whether the item has focus within the enclosing focus scope. If true, this item will gain active focus when the enclosing focus scope gains active focus. (当包围的focus scope得到active focus时候, 它也会得到 active focus)In the following example, input will be given active focus when scope gains active focus.
Rectangle {
     FocusScope {
         id: scope
         TextInput {
             id: input
             focus: true
         }
     }
}
For the purposes of this property, the scene as a whole is assumed to act like a focus scope. On a practical level, that means the following QML will give active focus to input on startup.
Rectangle {
     TextInput {
         id: input
         focus: true
     }
}
See also activeFocus and Keyboard Focus.

Keyboard Focus in QML

This problem is fundamentally one of visibility。这个问题是由于可见性引起的。

/////////////////////////////////////////////////////////////////////////////////////

例子:

import Qt 4.7

//example from:
//http://developer.qt.nokia.com/doc/qt-4.7/qml-focusscope.html
/*
    Blue border indicates scoped focus
    Black border indicates NOT scoped focus
    Red box indicates active focus
    Use arrow keys to navigate
*/
Rectangle {
    color: "white"
    480
    height: 480

    FocusScope {
        id: myScope
        focus: true

        Rectangle {
            height: 120
            420

            color: "transparent"
            border. 5
            border.color: myScope.activeFocus?"blue":"black"

            Rectangle {
                id: item1
                x: 10; y: 10
                100; height: 100; color: "green"
                border. 5
                border.color: activeFocus?"blue":"black"
                KeyNavigation.right: item2
                focus: true

                Rectangle {
                    50; height: 50; anchors.centerIn: parent
                    color: parent.focus?"red":"transparent"
                }
            }

            Rectangle {
                id: item2
                x: 310; y: 10
                100; height: 100; color: "green"
                border. 5
                border.color: activeFocus?"blue":"black"
                KeyNavigation.left: item1
                Keys.onDigit9Pressed: console.log("Top Right");

                Rectangle {
                    50; height: 50; anchors.centerIn: parent
                    color: parent.focus?"red":"transparent"
                }
            }
        }
        KeyNavigation.down: item3
    }

    Rectangle {
        id: item3
        x: 10; y: 300
        100; height: 100; color: "green"
        border. 5
        border.color: activeFocus?"blue":"black"

        KeyNavigation.up: myScope

        Rectangle {
            50; height: 50; anchors.centerIn: parent
            color: parent.focus?"red":"transparent"
        }
    }

}
//总结:activeFocus 才是真正的焦点
//focus只是说明当前component的哪个将要获取焦点

activeFocus是read-only的,所以要设置焦点要用focus。

http://developer.qt.nokia.com/forums/viewthread/3261

免责声明:文章转载自《qml focus相关》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux系统下MySql表名大小写敏感问题C#读取带命名空间的xml,xaml文件的解决方案下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

QML 解决button中添加了MouseArea,点击事件就不响应了的问题

ToolBar { RowLayout { anchors.fill: parent ToolButton { id: toolbutton text: qsTr("‹") onClicked: c...

[转载]震惊!QWidget竟然可以嵌入到QML中,QMl窗口句柄竟然是这样获取

  背景     记得在初学qml时,就被大佬告知Qml的实现有两种方式“view+item”和“engine+widow”,那么能不能将QWidget嵌入到QML中来呢,我收到的答案是不可以,原因是QML的窗口句柄获取不到,但是,要想用原来的win32解码时就需要用到窗口句柄,但是我辛辛苦苦学的qml又不想放弃,于是就绞尽脑汁的找寻办法,终于,黄天...

QML(一)HelloWorld

一、Qt Widgets、QML、Qt Quick 的区别 参考: 1. Qt Widgets、QML、Qt Quick 的区别 2. Qt Widgets 和 Qt Quick / QML 二、基础教程 参考: 1. 使用QML进行界面开发 2. QML 三言两语 三、HelloWorld 使用Qt Creator直接新建工程,有如下选项(我的没有Can...

DirectSound---捕获音频、Qml/C++ 集成交互

DirectSound的音频捕获原理和播放原理差不多,内部在一个缓冲区上循环写入捕获到的数据,并且提供notify通知功能。 1. 音频捕获 因为捕获流程和播放流程类似,我们就不在这里赘述了,只给出简单的函数引用和图示: 最后两个是音效捕获接口,用来控制Aec(回声消除)、Noise Fill(噪音填充)、Ns(噪音压制)的开启与关闭: HRESULT D...

QML动态创建自定义组件

一、动态加载和实例化对象:createComponent 例子1: ①、创建本地的QML文件,注意文件第一个字母要大写 ZStation.qml import QtQuick 2.5 Rectangle{ property string mName: "station" signal entered(string objName); sig...

【QML 动态对象】Loader动态加载组件

Loader 元素用来动态加载可见的 QML 组件,它可以加载一个 QML 文件(使用 source 属性)或者一个组件对象(使用 sourceComponent 属性)。 对于拖延组件的创建很是有用的:例如,当一个组件需要在要求的时候被创建,或者由于性能原因一个组件不应该被创建时。 Item { 200; height: 200...