天天看点

Qt文件保存对话框默认保存文件

在调用Qt文件对话框用来保存文件时,有时候希望提供默认的文件名,具体该如何做呢?

The file dialog’s working directory will be set to dir. If dir includes a file name, the file will be selected. Only files that match the filter are shown. The filter selected is set to selectedFilter. The parameters dir, selectedFilter, and filter may be empty strings. Multiple filters are separated with ‘;;’。

如果设置了dir,则文件对话框会将此dir当工作目录,如果dir包括文件名,则会选择该文件。只有当文件跟过滤相匹配时才显示,选定的过滤器设置为selectedFilter。参数dir、selectedFilter和filter可以是空字符串。多个过滤器用“;;”分隔。

也就是说当dir包含文件名时,此文件名就是默认的保存文件名。例如:

QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
                            "/home/jana/untitled.png",
                            tr("Images (*.png *.xpm *.jpg)"));
           

继续阅读