Qt/QML编程学习之心得:实现一个图片浏览器(十八)

QML中有个重要控件,经常使用就是image,通常可以用它来显示一张图片。如果想结合openfiledialog来让image显示图片,也就是做一个简易的图片浏览器,怎么弄呢?

DefaultFileDialog.qml:

import QtQuick 2.0
import QtQuick.Dialogs 1.0

FileDialog {
    id: fileDialog
    title: "Please choose a file"
    folder: shortcuts.home
    visible:false
    nameFilters: [ "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.0

ApplicationWindow {
    id:app
    visible: true
    width: 640
    height: 480
    title: qsTr(" World")
    objectName: "Appwindow"

你可能感兴趣的:(qt,qml)