Qt QFileDialog - 仅具有静态函数的本机对话框?

发布于 2024-08-28 06:44:35 字数 480 浏览 10 评论 0原文

我试图简单地保存一个文件。但是,我需要输入一个不带后缀的文件名来自动获取默认后缀(setDefaultSuffix() 会这样做)。

我不想为此完全失去本机保存对话框。 exec() 没有从 QDialog 重载,因此它完全绕过本机挂钩(忽略 DontUseNativeDialog 选项,即使它是 false)。

如果我禁用文件覆盖警告并在函数返回后自己附加默认后缀,那么如果用户不想覆盖,我将重新打开对话框......这太丑陋了。

是否有一些信号我可以捕获并快速注入默认后缀(如果不存在)?我猜不是,因为它是一个原生对话框。

我的过滤器有什么问题吗?我只有一种过滤器选择。它应该使用该扩展名。

这看起来很蹩脚。启动保存对话框并简单地键入“test”永远不会产生无扩展名的文件。 “测试。”,是的。 “测试”没办法。当用户点击“加载”并且看不到他们刚刚保存的文件时,这确实会让他们感到困惑。

我想 Qt 的跨平台部分给了我最低公分母文件对话框功能?

I'm trying to simply save a file. However, I need a filename entered without a suffix to automatically get a default suffix (which setDefaultSuffix() does).

I'd rather not completely lose the native save dialog just for this. exec() is not overloaded from QDialog, so it totally bypasses the native hook (ignoring the DontUseNativeDialog option even if it's false).

If I disable the file overwrite warning and append the default suffix myself after the function returns, then I'd be re-opening the dialog if the user did not want to overwrite... and that's just ugly.

Is there some signal I can catch and quickly inject the default suffix if it's not there? I'm guessing not, since it's a native dialog.

Is there something I'm doing wrong with the filter? I only have one filter choice. It should use that extension.

This seems pretty lame. Launching the save dialog and simply typing "test" should never result in an extensionless file. "test.", yes. "test" no way. That'll really confuse the users when they hit Load and can't see the file they just saved.

I guess the cross-platform part of Qt is giving me lowest common denominator file dialog functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海里的那抹蓝 2024-09-04 06:44:35

是的,如果您查看 Qt 源代码,很明显只有静态函数使用本机文件对话框。不幸的是,不可能以任何其他方式获得本机对话框......

Yes, if you look at the Qt source code it is evident that only the static functions uses native file dialogs. It is not possible to get native dialogs any other way, unfortunately...

闻呓 2024-09-04 06:44:35

您是否尝试过静态函数中的过滤器选项? [编辑:哎呀,注意到你已经有了。]

例如,我自己尝试过,事情似乎相当合理:

QString filter = "Text files (*.txt)";
QString selectedFilter;
QString filename = QFileDialog::getSaveFileName(0, "", "", filter, &selectedFilter);

在保存对话框中输入 test 返回 test.txt< /em>.
在保存对话框中输入 test. 将返回 test..txt
在保存对话框中输入 test.foo 将返回 test.foo

如果已经存在具有该名称的现有文件,这些都会显示相应的覆盖对话框。

我获得 test(不带任何后缀)的唯一方法是用引号括起来("test"),或者首先输入 *.* (这将使其显示所有文件),然后输入 test。 (尽管奇怪的是 selectedFilter 仍然包含对话框中显示的过滤器,即使它没有被使用)。

Have you tried the filter options in the static functions? [Edit: Oops, noticed that you already have.]

I just tried this myself, for example, and things seem to be fairly reasonable:

QString filter = "Text files (*.txt)";
QString selectedFilter;
QString filename = QFileDialog::getSaveFileName(0, "", "", filter, &selectedFilter);

Entering test in the save dialog returns test.txt.
Entering test. in the save dialog returns test..txt.
Entering test.foo in the save dialog returns test.foo.

These all show the appropriate overwrite dialog if there is already an existing file with that name.

The only way I can get test, without any suffixes, is by surrounding it with quotes ("test"), or by first entering *.* (which will make it display all files) and then entering test. (Although one oddity is that selectedFilter will still contain the filter shown in the dialog, even if it's not used).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文