处理qml事件

来源:互联网 发布:dwg软件免费下载 编辑:程序博客网 时间:2024/04/29 09:41

Qt Qml 引进了Signal概念用于事件系统。我们通常说的event在这里被命名为signal,event handler被称为signal handler。

官方文档在下面:

http://qt-project.org/doc/qt-5/qtqml-syntax-signals.html

下面是我的简单的例子。一个qml文件包含了输入文本和按钮。Qt允许我们用JavaScript处理接受到的signal.

import QtQuick 2.0import QtQuick.Controls 1.1import QtQuick.Controls.Styles 1.1import QtQuick.Dialogs 1.1Rectangle {    Rectangle {        id: inr        anchors.top: parent.top        anchors.left: parent.left        anchors.leftMargin: 5        anchors.topMargin: 10        width: parent.width * 0.8        height: parent.height * 0.4        border.color: "black"        border.width: 1        TextInput {            id: input_t1            text: "FileGDB path"            readOnly: true            anchors.top: parent.top            anchors.topMargin: 5            anchors.left: parent.left            anchors.leftMargin: 5            cursorVisible: false        }        Button {            id: input_t2            text: "Choose"            anchors.top: input_t1.bottom            anchors.topMargin: 5            anchors.left: parent.left            anchors.leftMargin: 5            onClicked: {                console.log("ok");            }        }    }}
在onClicked处理程序中,我写了一行JavaScript代码: console.log("ok").

让我们用命令测试一下:

 ~/Qt5.2.0/5.2.0/gcc_64/bin/qmlscene ./qmls/props/input/file_gdb.qml


现在点击一下按钮,你会看到一个字符串"ok"被打印在终端里。

上面是个简单的例子。现在,我在点击按钮的时候弹出一个文件对话框。添加下面的代码到qml文件中。

 FileDialog {            id: fileDialog            title: "Please choose a file"            onAccepted: {                input_t1.text = fileDialog.fileUrls[0]            }            onRejected: {                console.log("Canceled")            }        }

下面是一些截屏:



0 0
原创粉丝点击