简体   繁体   English

在 Qt 应用程序中传递文件路径的推荐方式

[英]Recommended way of passing file paths in Qt application

in the codebase (C++ and Qt5) I am working on, filepaths are passed as QString parameters most of the time.在我正在处理的代码库(C++ 和 Qt5)中,文件路径大部分时间都作为 QString 参数传递。 In some cases it is also a combination of QDir and QString.在某些情况下,它也是 QDir 和 QString 的组合。

Examples:例子:

void foo(const QString &filename);
void foo(const QDir pathToFile, const QString &filename);

In order to use stronger types for the interfaces, I was considering the following types:为了对接口使用更强的类型,我正在考虑以下类型:

  • QString字符串
  • QDir目录
  • QFile文件
  • QFileInfo QFileInfo
  • QUrl网址
  • std::filesystem::path标准::文件系统::路径

QString This is the simplest, with the disadvantage that the type itself does not even tell that it is a path. QString这是最简单的,缺点是类型本身甚至不告诉它是一条路径。

QDir Compared to QString we know that a path is passed. QDir与 QString 相比,我们知道传递了一个路径。 But still, it could be a path to a directory or a path to a file.但是,它仍然可以是目录的路径或文件的路径。 Also the name QDir seems to be misleading for passing paths to files.此外,名称 QDir 似乎对传递文件路径具有误导性。

QFile For QFile it is clear that it is about a file, not about a directory. QFile对于 QFile,很明显它是关于一个文件,而不是关于一个目录。 The thing I don't like about this is that it opens up other questions, such as "Should the passed file be opened before passing it? Does it need to exist?"我不喜欢的一点是它会引发其他问题,例如“传递的文件是否应该在传递之前打开?它需要存在吗?”

QFileInfo This seems to fit better than QFile, as it is a more lightweight class which is not used for modifying the file. QFileInfo这似乎比 QFile 更适合,因为它是一个更轻量级的类,不用于修改文件。

QUrl To me, this seems to be a too general-purpose class for passing file paths around. QUrl对我来说,这似乎是一个用于传递文件路径的通用类。

std::filesystem::path This looks like it is a good equivalent to QDir. std::filesystem::path这看起来与 QDir 相当。 What I like, is that the name does not suggest that it is a directory.我喜欢的是这个名字并不暗示它是一个目录。 However I am not sure if mixing Qt and std:: classes here could cause other troubles ie native file paths.但是我不确定在这里混合 Qt 和 std:: 类是否会导致其他问题,即本机文件路径。

Is there a recommended way for passing filepaths as parameters?是否有推荐的方法将文件路径作为参数传递? Any thoughts are highly appreciated.任何想法都受到高度赞赏。 My goal would be to have a guideline saying: When passing a path to a file, use class XYZ.我的目标是制定一个指导方针:将路径传递给文件时,使用类 XYZ。

As someone, who has worked both on and with Qt, I should be biased, but I will not be.作为一个既使用 Qt 又使用 Qt 的人,我应该有偏见,但我不会。 I will say that, in my opinion, always prefer to use the standard template library.我会说,在我看来,总是更喜欢使用标准模板库。

Even if you do not, always question yourself why not.即使您不这样做,也要始终问自己为什么不这样做。 And you need to have a good selling point.你需要有一个好的卖点。 Qt has been trying too hard to get rid of obsolete Qt APIs that have been replaced by the recent std features. Qt 一直在努力摆脱过时的 Qt API,这些 API 已被最近的 std 特性所取代。

So, unless you really have to, I would not go for the Qt equivalents, personally.因此,除非您真的必须这样做,否则我个人不会选择 Qt 等价物。

Now, of course, they are acceptable and OK if you have to confirm to an existing style.现在,当然,如果您必须确认现有样式,它们是可以接受的并且可以的。 But there is little to no reason to hold unto Qt APIs when they are replaced by standard interfaces in C++.但是当 Qt API 被 C++ 中的标准接口取代时,几乎没有理由坚持使用它们。

We just need to move on and thank the corresponding Qt APIs that they have served us really well for so long and have probably been good inspiration for forming the next generation standards.我们只需要继续前进并感谢相应的 Qt API,它们长期以来一直很好地为我们服务,并且可能是形​​成下一代标准的良好灵感。

I would like to make it clear that this is not against Qt or any Qt API in particular.我想明确指出,这并不针对 Qt 或任何 Qt API。 It is just life.这只是生活。 APIs also age with us. API 也随着我们而老化。 And eventually, the ideal situation is when we can use the most from the standard.最终,理想的情况是我们可以最大限度地利用标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM