如果我只想说标识符文件名,如何将 QRegexpValidator 附加到 QFileDialog?

How do I attach a QRegexpValidator to QFileDialog if I only want say identifier file names?

这个问题很简单,而且在标题中。

谷歌搜索对此没有帮助。如何让 QFileDialog 在其保存名称字段上使用 QValidator?

谢谢。

以下内容有点乱,但似乎有效。

您可以使用 QObject::findChildren 来定位对话框的 QLineEdit 子窗口小部件。假设只有一个这样的小部件,然后您可以将验证器应用于该小部件...

QFileDialog fd;
auto children = fd.findChildren<QLineEdit *>();
if (children.size() == 1) {

  /*
   * Apply a validator that forces the user to enter a name
   * beginning with a lower case `a' -- a bit pointless but...
   */
  QRegExpValidator validator(QRegExp("^a"));

  /*
   * Apply the validator.
   */
  children.front()->setValidator(&validator);
  fd.exec();
}

快速测试表明它似乎工作正常。就像我说的那样:感觉确实有点乱。