PDA

View Full Version : how to I restrict the change folder optionality in a file dialog



qt_user
23rd September 2010, 14:16
I open a "save file" dialog throgh the function call QFileDialog::getSaveFileName() .........
Now I want that the file which is to be saved should always get saved in the current file directory itself........what should I do for that?
Thanx in advance!!

tbscope
23rd September 2010, 14:30
By setting a filter

lyuts
24th September 2010, 15:07
tbscope, I guess filter impacts only the display of the files by their extensions (this is according to the QFileDialog's documentation).
qt_user, instead you can do something like this:


do {
QString filePath = QFileDialog::getSaveFileName(parent, caption, dir, filter);
} while (isFilePathAcceptable(filePath));


where isFilePathAcceptable is your function that implements the criteria for checking whether the file path is valid. Saying "current directory" may imply different paths, so only know what a proper check should be done.

tbscope
24th September 2010, 15:13
tbscope, I guess filter impacts only the display of the files by their extensions (this is according to the QFileDialog's documentation).
I have to be honest and say I don't really know, but the documentation of the QDir filters says this:


QDir::Dirs List directories that match the filters.
I assume that you can set a name filter and then set it to only show the dirs that match the name filter?

lyuts
25th September 2010, 20:52
I have to be honest and say I don't really know, but the documentation of the QDir filters says this:


I assume that you can set a name filter and then set it to only show the dirs that match the name filter?

That is all about QDir's filter but not QFileDialog's filter. In case of QDir it is understood that a filter influences the list of directories, but in case of QFileDialog it will co rrespondingly influence a list of files to display. But qt_user needs to restrict the list of directories, while using QFileDialog. That's why I assumed that the filter will not help him in this case.

SixDegrees
25th September 2010, 21:56
I don't understand the original post. If you're asking that the file always be saved in the "current directory," I assume you mean the directory where the program was started; in that case, just set the getSaveFileName() dir parameter to "./".

If you're asking how to always save the file in the same, fixed directory, the same approach can be taken, although you don't really need a file dialog for that; a simple dialog with a line edit asking for the filename is sufficient.

In either of the first instances, it will always be possible for the user to navigate somewhere else with the provided file dialog. Short of reimplementing the guts of the file dialog, you can simply check the returned filename, and if it isn't in the desired directory put up a warning and reject it.

lyuts
26th September 2010, 14:26
I don't understand the original post. If you're asking that the file always be saved in the "current directory," I assume you mean the directory where the program was started; in that case, just set the getSaveFileName() dir parameter to "./".


By setting the dir to "./" you specify the starting location for QFileDialog when it is opened, but that doesn't prohibit a user to change this directory to another one and save a file there, which will mean that the file is going to be saved in the wrong location. Do you agree with that?

wysota
26th September 2010, 15:23
I think you should build your own dialog instead of using QFileDialog and do not even bother the user with information it is tied to the filesystem. Just let him enter the filename and that's it.

SixDegrees
26th September 2010, 17:15
Yes; in fact, I made explicit mention of it in my post, along with a solution.

wysota
26th September 2010, 18:36
Yes; in fact, I made explicit mention of it in my post, along with a solution.

I admit I haven't read the whole thread :o

SixDegrees
26th September 2010, 21:18
That was in reply to lyuts, not you.

Although I did mention your solution, as well. ;)

wysota
27th September 2010, 12:06
Hmm... maybe I should switch to the threaded view of the forum :)