QtQuick用户交互:鼠标移动、点击、拖拽和键盘输入
1、事件处理
1)Qt使用信号槽的基础处理大部分(非所有)的时间相应问题
2)在QML,类似地当有事件发生时,一个与事件相关的信号会被发出。所以,要处理事件,需要定义一个槽。这个槽仅仅是一个属性(property);这个属性的名字与事件的类型是相关的(鼠标点击、计时、键盘按键等等)
2、鼠标区域(Mouse Area)
1)Mouse Area用于定义屏幕的某区域接收鼠标事件,位置与大小,与普通的items是一样的(可以对Mouse Area使用anchors)
2)两种方法处理鼠标输入:处理信号和动态属性绑定
3、与鼠标事件相关的信号
onClicked、onDoubleClicked、onPressedAndHold、onReleased等等。如:
Rectangle {
...
acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: {
if(mouse.button == Qt.RightButton)
parent.color = "blue"
else
parent.color = "red"
}
...
}
4、拖拽元素
通过设置MouseArea的属性drag,可以让某个元素被拖拽。如:
Rectangle {id: opacitytest;...Image {id: pic;MouseArea {anchors.fill: parentdrag.target: picdrag.axis: "XAxis"drag.minimumX: 0drag.maximumX: opacitytest.width - pic.width}}...}5、鼠标悬停与属性Rectangle {...color: mouse_area.containsMouse ? "green" : "blue"MouseArea {id: mouse_areaanchors.fill: parenthoverEnabled: true}...}6、鼠标区域的注意点1)鼠标区域只会相应acceptedButtons所定义的鼠标按键,槽不会接收到其他的鼠标按键事件;多个鼠标按键被按下时,pressedButtons属性会记录所有的按键,若有acceptedButtons指定的按钮被按下,没有指定的按钮也会被记录2)当hoverEnabled为false时,鼠标按下,containsMouse为true7、信号VS属性绑定1)当一个信号只影响到某个元素时,信号更容易使用2)属性绑定只能针对有id的元素使用,多个元素可以对同一个鼠标事件做出相应8、键盘输入1)接收文本输入:TextInput and TextEdit(different: TextEdit is multi-line)2)在元素之间导航:a)改变元素的焦点;b)导航键(arrows keys),tab and backtab3)按键输入:相应任意的按键9、改变焦点1)UIs只有一个TextInput的时候,焦点自动在TextInput上2)若有多个TextInput,需要通过鼠标点击来改变焦点3)若TextInput没有文字,鼠标无法点击,除非他有width属性或通过anchors布局过4)通过设置focus属性来改变焦点10、焦点导航TextInput {id: name_field...focus: trueKeyNavigation.tab: address_field}TextInput {id: address_field...KeyNavigation.backtab: name_field}1)id为name_field的元素定义了KeyNavigation.tab,当按Tab键的时候焦点就移动到了address_field上2)id为address_field的元素定义了KeyNavigation.backtab,当按shift+Tab键的时候焦点移动到了name_field上11、按键导航为non-text元素使用导航键,non-text元素也可以有焦点
1)
Rectangle {//设置宽、高、颜色等属性Rectangle {id: leftRectcolor: focus ? "red" : "darkred"KeyNavigation.right: rightRectfocus: true}Rectangle {id: rightRectcolor: focus ? "#00ff00" : "green"KeyNavigation.left: leftRect}}2)图片旋转的例子Rectangle {width: 400; height: 400; color: "black"Image {id: homex: 150; y: 150source: "images/home01.jpg"transformOrigin: Item.Center}Keys.onLeftPressed:home.rotation = (home.rotation - 10) % 360Keys.onRightPressed:home.rotation = (home.rotation + 10) % 360focus: true}This property-transformOrigin-holds the origin point around which scale and rotation transformNine transform origin are available. The default transform origin is Item.CenterThe nine transform origin are TopLeft, Top, TopRight, Left, Center, Right, BottomLeft, Bottom, BottomRight
12、键盘按键输入
1)所有可是元素都可以通过Keys的attached属性进行键盘事件处理
2)支持的键盘事件包括:A)通用事件(onPressed、onReleased);B)专用事件(onReturnPressed、onSelectPressed、
onVolumePressed等等,他们都有个类型为KeyEvent的参数event)
3)处理通用信号时,需要显示的告知事件被处理了,event.accepted = true;否则这个事件将会传递
4)专用事件默认就将事件处理了
Item {//Handle a key event with a generic handlerfocus: trueKeys.onPressed: {if(event.key == Qt.Key_Left){//See Qt::Key for codesconsole.log("move left")event.accepted = true//Must accept explicity}}}Item {//Handle a key event with a specialized handlerfocus: trueKeys.onLeftPressed://Accepts the event by defaultconsole.log("move left")}
本文介绍QtQuick中如何处理用户交互,包括鼠标事件处理、键盘输入响应及焦点导航等内容。通过MouseArea组件定义鼠标响应区域,并利用信号和属性绑定实现交互逻辑。同时探讨了键盘输入的处理方法。
1838

被折叠的 条评论
为什么被折叠?



