问题描述
我尝试在Mac OS X中创建另存为..."对话框.但是我不想使用QFileDialog::getSaveFileName()
函数,因为由该函数创建的对话框实际上是 NOT Mac OS X Lion中为-native.因此,我决定将对话框创建为QFileDialog
对象:
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 );
一切正常,除了一个问题.我无法为保存的文件设置默认名称,因此用户必须每次手动键入此名称.我知道函数QFileDialog::getSaveFileName()
允许通过第三个参数 dir ( http://qt-project.org/doc/qt-4.8/qfiledialog.html#getSaveFileName ).但是如何在不使用此功能的情况下设置默认名称?
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?
我可以通过QFileDialog::setDefaultSuffix()
函数为保存的文件设置默认后缀,但是我需要设置整个默认名称,而不仅仅是默认后缀.
I can set default suffix for saved file via QFileDialog::setDefaultSuffix()
function, but I need to set whole default name, not only default suffix.
我尝试使用QFileDialog::setDirectory()
函数,但是它仅设置要保存的目录,而没有保存文件的名称.
I've tried to use QFileDialog::setDirectory()
function, but it sets only directory where to save, without name of saved file.
我在Mac OS X Lion上使用Qt 4.8.1.
I use Qt 4.8.1 on Mac OS X Lion.
预先感谢您的帮助.
推荐答案
我在Google上搜索了set default filename qfiledialog
,并在整个讨论中发生了.
I searched google for set default filename qfiledialog
and happened across this discussion.
我发现只有在文件确实存在的情况下,使用 selectFile("myFileName");
才有效.就我而言,目的是创建一个新文件,并可以选择覆盖现有文件.
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.
对我来说有效的解决方案(Qt 5.3.2)如下:
The solution that worked for me (Qt 5.3.2) was as follows:
QFileDialog svDlg;
QString saveFileName = svDlg.getSaveFileName(this, caption, preferredName, filter);
在上面的示例中,preferredName是一个QString,其中包含"C:/pre-selected-name.txt"
In the above example, preferredName is a QString that contains "C:/pre-selected-name.txt"
希望有帮助
这篇关于QFileDialog:如何在“另存为..."中设置默认文件名;对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!