没有办法在Linux上使用QFileDialog自动向文件添加扩

没有办法在Linux上使用QFileDialog自动向文件添加扩

本文介绍了有没有办法在Linux上使用QFileDialog自动向文件添加扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够输入将被保存为xml文件的文件的名称。目前在Windows和Mac如果输入测试作为文件名,它会自动添加.xml这是我想要的。不幸的是,测试一个Linux构建时,我发现输入一个没有扩展名的文件名将保存为普通文件。用户必须在文件字符串中指定扩展名(即test.xml),以便以正确的格式保存。

I want the user to be able to enter a name for a file that will be saved as an xml file. Currently on Windows and Mac if you enter "test" as the file name it will automatically add ".xml" which is what I want. Unfortunately when testing a Linux build I found that entering a file name without an extension would save as an ordinary file. The user has to specify the extension in the file string (i.e "test.xml") in order for it to save in the correct format.

使用如下。这是一个Qt错误或有一种方法来指定在Qt它应该添加一个扩展,如果没有找到?

The code I'm using is below. Is this a Qt bug or is there a way to specify in Qt that it should add an extension if one wasn't found?

// Get value for "dir". If the setting doesn't exist then use
// the the path in "defaultsave.directory"
QString prevPath = prevValues.value("dir", QString::fromStdString(
  ConfigService::Instance().getString("defaultsave.directory"))).toString();

QString filter;
filter.append("Files (*.xml)");
filter += ";;AllFiles (*.*)";
QString groupingFile = QFileDialog::getSaveFileName(this, "Save Grouping file as", prevPath, filter);


推荐答案

字符串从 getSaveFileName 的对话框中,你可以做类似的操作:

Since you get the string from the dialog with getSaveFileName, you can just do something like:

if (!groupingFile.endsWith(".xml"))
    groupingFile += ".xml";

这在Linux上可能不同,因为这个小片段埋在:

It's probably different on Linux because of this little snippet buried in the documentation for getSaveFileName:

换句话说,它是添加的前缀(由本地对话框完成)是异常的,至少在Qt。

In other words, it's the adding of the prefix (done by the native dialogs) that is aberrant, at least in terms of Qt.

正如评论中所指出的,您可能会发现此解决方案存在问题,因为如果您键入时,对话框本身不会通知您 xyzzy ,并且文件 xyzzy.xml 已存在(假设原生对话框执行此操作 - )。如果你想要这种行为,你也需要实现它。

As pointed out in the comments, you may find an issue with this solution in that the dialog box itself won't notify you if you type in xyzzy manually and the file xyzzy.xml already exists (assuming the native dialogs do this - I haven't actually checked). If you want that behaviour, you'll need to implement it as well.

这篇关于有没有办法在Linux上使用QFileDialog自动向文件添加扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:08