QT之鼠标、键盘、定时器

xiaoxiao2021-02-28  93

用简单的例子说话

import QtQuick 2.7 import QtQuick.Controls 2.0 Rectangle { visible: true; width: 640; height: 480; color: "#DCDCDC" MouseArea { anchors.fill: parent onClicked: { Qt.quit(); } } focus: true; Keys.enabled: true; Keys.onPressed: { if (event.key == Qt.Key_Escape){ Qt.quit(); } } Text { id: text_date anchors.centerIn: parent color: "#FF0000" font.pointSize: 24 } Timer { id: timer_date interval: 1000; repeat: true; triggeredOnStart: false running: true onTriggered: { text_date.text = Date().toString(); } } }

将上述代码保存在01.qml文件中,使用qmlscene 01.qml可以看到如下效果: 定时器每秒刷新日期时间,使用鼠标单击或者按键Esc键都可以退出应用程序

此程序虽简单,确很清楚的表示了鼠标、按键、定时器三种事件响应 鼠标使用MouseArea 、定时器使用Timer 、按键则使用附加属性和附加信号处理器Keys

ithewei 认证博客专家 c/c Qt libhv 编程之路,其路漫漫,吾将上下而求索https://github.com/itheweihttps://hewei.blog.csdn.net
转载请注明原文地址: https://www.6miu.com/read-34168.html

最新回复(0)