QML之FileDialog
FileDialog是QtQuick中的文件对话框,它可以选择已有的文件,文件夹(可以多选)也可以在保存文件的时候让用户提供名字来创建文件或文件夹。
FileDialog的实现是和平台相关的,如果没有可用的原生文件对话框,则会尝试创建一个QFileDialog,如果失败,怎使用默认的QML对话框。
要想在QtQuick中使用FileDialog,首先我们得导入包吧。。
import QtQuick.Dialogs 1.2
下面让我们看下filedialog都有哪些常用的属性:
title:对话框标题
folder:用户选择的文件目录
selectExisting:选择已有文件或文件夹,默认值为true。当设置为false时候意味着创建文件或者文件夹
selectFolder:表示选择文件,默认值为false。当为true的时候表示选择文件夹
selectMultiple:默认值false,表示单选。为true时表示多选。设置该属性selectExisting的值必须为true
nameFilters:文件过滤列表
fileUrl:选择文件路径
fileUrls:选择文件路径列表
下面看下实例:
Button {
id:openBtn
height: 25
text:qsTr("浏览...")
anchors.leftMargin: 10
onClicked: {
fds.open();
}
}
Label {
id: labels
text: qsTr("")
height: 25
anchors.left:openBtn.right
anchors.leftMargin: 10
}
FileDialog {
id:fds
title: "选择文件"
folder: shortcuts.desktop
selectExisting: true
selectFolder: false
selectMultiple: false
nameFilters: ["json文件 (*.json)"]
onAccepted: {
labels.text = fds.fileUrl;
console.log("You chose: " + fds.fileUrl);
}
onRejected: {
labels.text = "";
console.log("Canceled");
Qt.quit();
}
}
截图:
log输出:
qml: You chose: file:///C:/Users/liuwenqiang/Desktop/ani/fly2.json
qml: Canceled
还没有评论,来说两句吧...