Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QFileDialog: how to set default filename in "Save as..." dialog

I try to create "Save as..." dialog in Mac OS X. But I don't want to use QFileDialog::getSaveFileName() function, because dialog that created by this function is NOT truly-native in Mac OS X Lion. So I decide to create dialog as QFileDialog object:

auto export_dialog( new QFileDialog( main_window ) );
export_dialog->setWindowModality( Qt::WindowModal );
export_dialog->setFileMode( QFileDialog::AnyFile );
export_dialog->setAcceptMode( QFileDialog::AcceptSave );

All works fine, except one problem. I cannot set default name for saved file, so user must type this name manually every time. I know that function QFileDialog::getSaveFileName() allows to set default filename via third argument, dir (http://qt-project.org/doc/qt-4.8/qfiledialog.html#getSaveFileName). But how to set this default name without this function?

I can set default suffix for saved file via QFileDialog::setDefaultSuffix() function, but I need to set whole default name, not only default suffix.

I've tried to use QFileDialog::setDirectory() function, but it sets only directory where to save, without name of saved file.

I use Qt 4.8.1 on Mac OS X Lion.

like image 527
Denis Shevchenko Avatar asked Mar 14 '13 17:03

Denis Shevchenko


1 Answers

I have found that using selectFile("myFileName"); only works if the file actually exists. In my case, the intent is to create a new file with the option of overwriting an existing file.

The solution that worked for me (Qt 5.3.2) was as follows:

QFileDialog svDlg;

QString saveFileName = svDlg.getSaveFileName(this, caption, preferredName, filter);

In the above example, preferredName is a QString that contains "C:/pre-selected-name.txt"

like image 153
Louis Parkin Avatar answered Sep 19 '22 16:09

Louis Parkin