Problem with FileDialog in QML

1

I'm trying to use the FileDialog component in QML

I've done exactly the same code that is in the Qt documentation on the link and this code did not show FileDialog and returned the error: QFileInfo::absolutePath: Constructed with empty filename . I tried to write a simple code to test, but the same error occurred. My code is below:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2

Window {
    visible: true

    width: 360
    height: 640

    maximumHeight: 640
    minimumHeight: 640

    maximumWidth: 360
    minimumWidth: 360

    title: "Acessar Galeria Test"

    Rectangle {
        id: principal

        anchors.fill: parent

        FileDialog {
            id: fileDialog

            title: "Please choose a file"

            folder: shortcuts.home

            visible: true
        }
    }
}
    
asked by anonymous 18.11.2015 / 14:40

1 answer

1
The propriedade visible of FileDialog can not be true while the component is not complete, so you must use Component.onComplete to set FileDialog to true . So the code should look like this to work:

import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2

Window {
    visible: true

    width: 360
    height: 640

    maximumHeight: 640
    minimumHeight: 640

    maximumWidth: 360
    minimumWidth: 360

    title: "Acessar Galeria Test"

    Rectangle {
        id: principal

        anchors.fill: parent

        FileDialog {
            id: fileDialog

            title: "Please choose a file"

            folder: shortcuts.home

            visible: false
        }
    }

    Component.onCompleted: {
        fileDialog.visible = true;
    }
}
    
18.11.2015 / 17:54