Is there a way to automatically add extensions to a file using QFileDialog on Linux

空扰寡人 提交于 2019-12-04 03:37:51

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.

We created our own save dialog method to get around this. The solution is essentially the same as @paxdiablo's answer (just add the extension), but this is more general:

QString ShowSaveFileDialog(QWidget *parent,
                           const QString &title,
                           const QString &directory,
                           const QString &filter) {
#if defined(Q_WS_WIN) || defined(Q_WS_MAC)
  return QFileDialog::getSaveFileName(parent,
                                      title,
                                      directory,
                                      filter);
#else
  QFileDialog dialog(parent, title, directory, filter);
  if (parent) {
    dialog.setWindowModality(Qt::WindowModal);
  }
  QRegExp filter_regex(QLatin1String("(?:^\\*\\.(?!.*\\()|\\(\\*\\.)(\\w+)"));
  QStringList filters = filter.split(QLatin1String(";;"));
  if (!filters.isEmpty()) {
    dialog.setNameFilter(filters.first());
    if (filter_regex.indexIn(filters.first()) != -1) {
      dialog.setDefaultSuffix(filter_regex.cap(1));
    }
  }
  dialog.setAcceptMode(QFileDialog::AcceptSave);
  if (dialog.exec() == QDialog::Accepted) {
    QString file_name = dialog.selectedFiles().first();
    QFileInfo info(file_name);
    if (info.suffix().isEmpty() && !dialog.selectedNameFilter().isEmpty()) {
      if (filter_regex.indexIn(dialog.selectedNameFilter()) != -1) {
        QString extension = filter_regex.cap(1);
        file_name += QLatin1String(".") + extension;
      }
    }
    return file_name;
  } else {
    return QString();
  }
#endif  // Q_WS_MAC || Q_WS_WIN
}

Did you look at QFileDialog::setDefaultSuffix?

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