QT之鼠标、键盘、定时器

来源:互联网 发布:magnet 磁链下载 软件 编辑:程序博客网 时间:2024/06/05 10:07

用简单的例子说话

import QtQuick 2.7import QtQuick.Controls 2.0Rectangle {    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可以看到如下效果:
01qml
定时器每秒刷新日期时间,使用鼠标单击或者按键Esc键都可以退出应用程序

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

原创粉丝点击