Qml实现对鼠标和键盘事件的简单处理
2016-03-11 21:57
716 查看
import QtQuick 2.3 import QtQuick.Window 2.2 import QtQuick.Controls 1.4 //实现对鼠标事件的处理和对文本的移动处理(键盘事件) Window { id:main; visible: true; MouseArea { acceptedButtons: Qt.LeftButton|Qt.RightButton;//确定接收哪些事件 anchors.fill: parent; onClicked: { if(mouse.button==Qt.LeftButton)//鼠标事件 { text.text="Leftbutton clicked"; } else if(mouse.button==Qt.RightButton) { text.text="Rightbutton clicked"; } } } Text { id:text; focus: true; x:50; y:50; anchors.bottom: t.bottom; Keys.enabled: true;//设置键盘可用 Keys.onPressed: { switch(event.key){//对键盘事件进行处理 case Qt.Key_Left: x-=10; event.accepted=true;//对接受到事件处理,避免再次向上传递 break; case Qt.Key_Right: x+=10; event.accepted=true; break; case Qt.Key_Up: y-=10;//因为原点在窗口的左上角 event.accepted=true; break; case Qt.Key_Down: y+=10; event.accepted=true; break; default:return } } color: "blue"; text: "hello world"; font.bold: true; font.pointSize: 16; styleColor: "#f51515"; verticalAlignment: Text.AlignVCenter; horizontalAlignment: Text.AlignHCenter; ColorAnimation on color { to: "black"; duration: 2000; } } }
相关文章推荐
- [置顶] Android第三方库学习清单
- android launchmode
- java笔记-10
- 改善视疲劳,句好眼镜验光师建议多吃水果,IT大佬们更应注意
- 关于Android app的launcher图标更换后,仍然显示默认的ic_launcher图标的解决方法
- 02-百思不得姐(第二天)
- java中初始化与构造器
- Linux下如何查看高CPU占用率线程 LINUX CPU利用率计算(转)
- CSS 中 Font-Family 中英文对照表
- oracle完整卸载
- java笔记-9
- Unity插件 DOTween基础
- CodeForces-630 F. Selection of Personnel【排列组合】
- 实现用画笔画出一个带有渐变效果的线条
- TCP协议中的SO_LINGER选项
- SQLite清空表并将自增列归零
- 最全SpringMVC具体演示样例实战教程
- 使用Python保存屏幕截图(不使用PIL)
- java练习 - 字符串反转
- web.xml