11

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)

Simon Warta
  • 8,469
  • 4
  • 32
  • 62

3 Answers3

5

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

Simon Warta
  • 8,469
  • 4
  • 32
  • 62
  • 2
    He used private QT headers, which breaks when upgrading QT – linquize May 03 '14 at 03:54
  • Might breakt, jap. But I don't have any other chance. I'll try to keep it up-to-date since we're using it in our software. – Simon Warta Jun 24 '14 at 05:18
  • In this post widgets module is used so I cannot get why he didn't create a wrapper above QFileDialog which has selectFile method? – VALOD9 Jan 22 '16 at 08:24
  • @VALOD9 I tried hard to do that. But I did not have a QWidget available that I could use as a parent. Without a parent, modality does not work. – Simon Warta Jan 22 '16 at 08:38
  • @SimonWarta I've just added a project sample to GitHub which demonstrates this possibility. And this project doesn't use private headers. Tested on Windows and Linux. – VALOD9 Jan 22 '16 at 10:12
  • @VALOD9 okay, where? I'd like to have a look at it. Feel free to add it as an alternative answer here. – Simon Warta Jan 22 '16 at 10:15
  • @SimonWarta sorry. I forgot to add a link :) – VALOD9 Jan 22 '16 at 10:17
  • @SimonWarta link is posted as an answer. – VALOD9 Jan 22 '16 at 10:19
3

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
        }
Marco Medri
  • 146
  • 3
1

Try setting the FileDialog selectExisting property to false.

jadev
  • 31
  • 1