Qt QFileDialog - native dialogs only with static functions?

二次信任 提交于 2021-02-07 13:16:52

问题


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?


回答1:


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...




回答2:


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).



来源:https://stackoverflow.com/questions/2609119/qt-qfiledialog-native-dialogs-only-with-static-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!