我希望用户能够为将被保存为xml文件的文件输入名称。 目前在Windows和Mac上,如果您输入“test”作为文件名,它将自动添加“.xml”,这正是我想要的。 不幸的是,当测试一个Linux版本时,我发现输入一个没有扩展名的文件名将会保存为普通文件。 用户必须在文件字符串中指定扩展名(即“test.xml”),以便以正确的格式进行保存。
我使用的代码如下。 这是一个Qt错误还是有没有办法在Qt中指定它应该添加一个扩展(如果没有找到)?
// 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);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.
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的对话框中获得了字符串,你可以像这样做:
if (!groupingFile.endsWith(".xml")) groupingFile += ".xml";这在Linux上可能有所不同,因为这段代码隐藏在getSaveFileName的文档中:
在Windows,Mac OS X和Symbian ^ 3上,此静态函数将使用本机文件对话框而不是QFileDialog。
换句话说,就是增加了前缀(由本机对话框完成),这是异常的,至少在Qt方面是这样的。
正如在评论中指出的那样,您可能会发现这个解决方案的一个问题,如果您手动输入xyzzy并且文件xyzzy.xml已经存在,对话框本身不会通知您(假设本机对话框这样做 - 我没有实际上没有检查)。 如果你想要这种行为,你也需要实现它。
Since you get the string from the dialog with getSaveFileName, you can just do something like:
if (!groupingFile.endsWith(".xml")) groupingFile += ".xml";It's probably different on Linux because of this little snippet buried in the documentation for getSaveFileName:
On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog.
In other words, it's the adding of the prefix (done by the native dialogs) that is aberrant, at least in terms of Qt.
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自动添加扩展名到文件(Is there a way to automatically add extensions to a file using QFileDialog on Linux)我希望用户能够为将被保存为xml文件的文件输入名称。 目前在Windows和Mac上,如果您输入“test”作为文件名,它将自动添加“.xml”,这正是我想要的。 不幸的是,当测试一个Linux版本时,我发现输入一个没有扩展名的文件名将会保存为普通文件。 用户必须在文件字符串中指定扩展名(即“test.xml”),以便以正确的格式进行保存。
我使用的代码如下。 这是一个Qt错误还是有没有办法在Qt中指定它应该添加一个扩展(如果没有找到)?
// 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);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.
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的对话框中获得了字符串,你可以像这样做:
if (!groupingFile.endsWith(".xml")) groupingFile += ".xml";这在Linux上可能有所不同,因为这段代码隐藏在getSaveFileName的文档中:
在Windows,Mac OS X和Symbian ^ 3上,此静态函数将使用本机文件对话框而不是QFileDialog。
换句话说,就是增加了前缀(由本机对话框完成),这是异常的,至少在Qt方面是这样的。
正如在评论中指出的那样,您可能会发现这个解决方案的一个问题,如果您手动输入xyzzy并且文件xyzzy.xml已经存在,对话框本身不会通知您(假设本机对话框这样做 - 我没有实际上没有检查)。 如果你想要这种行为,你也需要实现它。
Since you get the string from the dialog with getSaveFileName, you can just do something like:
if (!groupingFile.endsWith(".xml")) groupingFile += ".xml";It's probably different on Linux because of this little snippet buried in the documentation for getSaveFileName:
On Windows, Mac OS X and Symbian^3, this static function will use the native file dialog and not a QFileDialog.
In other words, it's the adding of the prefix (done by the native dialogs) that is aberrant, at least in terms of Qt.
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.
发布评论