3

I would like to save a file using QFileDialog::getSaveFileName without allowing to choose the path or at least hide folders. Reading the doc I tried to set QFileDialog::ShowDirsOnly as an option like this:

QString fileName = QFileDialog::getSaveFileName(this,
                                                tr("..."), path, tr("... (*.txt)"), 0, QFileDialog::ShowDirsOnly);

but it's not working or it's not what I want.

Is there a way to save a file without being able to seek a location in the system and using QFileDialog::getSaveFileName please? Or do I need to build my own QFileDialog where I will just write the name of the file?

I hope someone will understand my problem.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Jeanstackamort
  • 309
  • 2
  • 4
  • 16
  • Why do you need file dialog for this if you only prefer the user to select a file name? I would not use QFileDialog for that personally, but just a custom input box. – László Papp Mar 08 '14 at 02:55
  • This is what I have done so far but I would like something aesthetic. And implement this using `getSaveFileName` will ask before erase a file having the same name. – Jeanstackamort Mar 11 '14 at 16:11
  • You can do that with an input and message box. It does not seem you would like to have a QFileDialog. You seem that you would like to have a custom input box. – László Papp Mar 11 '14 at 16:20
  • Yes this is what I am finally doing. I just thought I could avoid this. Thank you for your help. – Jeanstackamort Mar 11 '14 at 16:42
  • Yep, understood. By the way, consider QML for the future. :-) – László Papp Mar 11 '14 at 16:54

1 Answers1

0

As indicated in the comment, you are looking for a bit specific behavior which is not quite a QFileDialog, albeit somewhat similar, admittedly.

I believe it is not possible to shrink this feature down from QFileDialog, so I would personally go for implementing my own input box with the required sanity check in the background.

The QFileDialog implementation might give some hint for such checks, but it is not vital to see the source code of it as these are relatively simple operations.

You could then provide warning and error messages with message box. That being said, it might be more beneficial for Qt 5 to consider QML for such operations.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • How would QML help here and not look “foreign” in a widgets application? I’d also point to QFileSystemModel, as it could be used for the custom selection dialog. (which would also be my suggestion) – Frank Osterfeld Mar 11 '14 at 20:30
  • @Frank: 1) I do not see widgets mentioned in the question other than QFileDialog. 2) Even if it was, putting this together in qml is simpler IMHO. 3) QML != !widgets; qml is just a language. Some people confuse it with QtQuick(Controls) though. It is possible to write widget based applications in QML. 4) QFSM is just a model, and not a view. 5) I am not sure you would need that here for this simple case. It is a bit too heavy IMHO. – László Papp Mar 11 '14 at 20:36
  • QListView + QFSM, how can it be easier than that? Ok, at least until you need proxy models ;). For a single folder, just QDir plus list view is probably easier indeed if one isn’t familiar with model/view. And yes, I know that QML != QtQuick and that it is conceptually possible to combine QML + widgets, it’s just that there’s no built-in support for that in Qt, so it’s rather unlikely that user2886875 is going that route. – Frank Osterfeld Mar 11 '14 at 20:42
  • With "conceptually possible" I'm referring to http://www.youtube.com/watch?v=NqpJEj15t9Q , btw. – Frank Osterfeld Mar 11 '14 at 21:03
  • @Frank: it is easier to have a line input and run the validation on "OK", and for that matter QML and QtQuick(Controls) are future proof; qtwidgets is not. – László Papp Mar 11 '14 at 23:47