FileDialog in QTQuick (QML): Save file under given name

社会主义新天地 提交于 2019-12-22 04:29:12

问题


We're building a Qt Quick app, that must be able to save a file under a given name.

In the FileDialog component you can only set a directory. This is not very user-friendly, since you don't want to type in a filename by hand every time you download a file.

So far we tried different things

  1. FileDialog from QtQuick.Dialogs: filename cannot be set

  2. Native dialog via QPlatformFileDialogHelper (naughty private c++ hack): filename cannot be set on Linux (Gnome)

  3. Native dialog via static QFileDialog::getSaveFileName(): in Quick application there is no QWidget available for 'parent'

  4. QT dialog via QFileDialog instance: Modality doeas not work, since 'parent' is not set. In Quick application there is no QWidget available for setParent() call

(Using C++ with QT 5.1 and QtQuick 2.1 incl. all desktop components)


回答1:


This blog post covers the whole problem and provides a possible solution: Advanced FileDialog in QML (Save file under given name)




回答2:


I hope this will be still helpful. I found a compromise that at least works for me. I used Qt.labs.platform 1.1 FileDialog QML Type: https://doc.qt.io/qt-5/qml-qt-labs-platform-filedialog.html

FileDialog {
            id: saveDialog
            property MyClass myObj
            title: "Save Dialog"
            folder: myObjHasAPath? myObj.path: "file:///" //Here you can set your default folder
            currentFile: "file:///"+myObj.name //The name of the item that you want to save
            fileMode: Platform.FileDialog.SaveFile
        }



回答3:


Try setting the FileDialog selectExisting property to false.



来源:https://stackoverflow.com/questions/20302838/filedialog-in-qtquick-qml-save-file-under-given-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!