且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

QFileDialog:如何在“另存为..."中设置默认文件名;对话

更新时间:2022-04-17 08:51:29

我在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"

希望有帮助