QML中有个重要控件,经常使用就是image,通常可以用它来显示一张图片。如果想结合openfiledialog来让image显示图片,也就是做一个简易的图片浏览器,怎么弄呢?
DefaultFileDialog.qml:
import QtQuick 2.0
import QtQuick.Dialogs 1.0FileDialog {id: fileDialogtitle: "Please choose a file"folder: shortcuts.homevisible:falsenameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]onAccepted: {console.log("You chose: " + fileDialog.fileUrls)imageViewer.source = fileDialog.fileUrl}onRejected: {console.log("Canceled")Qt.quit()}Component.onCompleted: visible = true
}
main.qml:
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.0ApplicationWindow {id:appvisible: truewidth: 640height: 480title: qsTr(" World")objectName: "Appwindow"