QFileSelector provides a convenient way of selecting file variants. 更多...
头: | #include <QFileSelector> |
qmake: | QT += core |
Since: | Qt 5.2 |
继承: | QObject |
QFileSelector (QObject * parent = Q_NULLPTR) | |
~QFileSelector () | |
QStringList | allSelectors () const |
QStringList | extraSelectors () const |
QString | select (const QString & filePath ) const |
QUrl | select (const QUrl & filePath ) const |
void | setExtraSelectors (const QStringList & list ) |
QFileSelector provides a convenient way of selecting file variants.
QFileSelector is a convenience for selecting file variants based on platform or device characteristics. This allows you to develop and deploy one codebase containing all the different variants more easily in some circumstances, such as when the correct variant cannot be determined during the deploy step.
If you always use the same file you do not need to use QFileSelector .
考虑以下范例用法,若希望在不同区域设置使用不同设置文件。可以在区域设置之间选择代码,像这样:
QString defaultsBasePath = "data/"; QString defaultsPath = defaultsBasePath + "defaults.conf"; QString localizedPath = defaultsBasePath + QString("%1/defaults.conf").arg(QLocale().name()); if (QFile::exists(localizedPath)) defaultsPath = localizedPath; QFile defaults(defaultsPath);
同样,若想要基于目标平台拾取不同数据文件,代码可能看起来像这样:
QString defaultsPath = "data/defaults.conf"; #if defined(Q_OS_ANDROID) defaultsPath = "data/android/defaults.conf"; #elif defined(Q_OS_IOS) defaultsPath = "data/ios/defaults.conf"; #endif QFile defaults(defaultsPath);
QFileSelector provides a convenient alternative to writing such boilerplate code, and in the latter case it allows you to start using an platform-specific configuration without a recompile. QFileSelector also allows for chaining of multiple selectors in a convenient way, for example selecting a different file only on certain combinations of platform and locale. For example, to select based on platform and/or locale, the code is as follows:
QFileSelector selector; QFile defaultsFile(selector.select("data/defaults.conf"));
要选择的文件放在的目录命名采用
'+'
和选择器名称。在以上范例中,通过将它们放置在以下位置选择平台配置:
data/defaults.conf data/+android/defaults.conf data/+ios/+en_GB/defaults.conf
To find selected files, QFileSelector looks in the same directory as the base file. If there are any directories of the form +<selector> with an active selector, QFileSelector will prefer a file with the same file name from that directory over the base file. These directories can be nested to check against multiple selectors, for example:
images/background.png images/+android/+en_GB/background.png
有了这些文件,可以在 Android 平台选择不同文件,但前提是语言环境为 en_GB。
对于不存在有效选择器的错误处理,推荐在基文件位置下存在默认文件或错误处理文件,即使期望所有部署都存在选择器。
在未来版本中,某些可能会被标记为部署时静态,并在部署步骤期间因优化而移动。由于选择器自带性能开销,推荐避免将它们用于涉及性能关键代码的情况。
选择器通常可用于的
进一步,选择器将添加自
QT_FILE_SELECTORS
环境变量,设置时应该是以逗号分隔的一组选择器。注意,只会读取此变量一次;选择器可能不更新,若变量在应用程序运行时改变。仅在首次使用时,评估初始选择器集一次。
还可以在运行时,为自定义行为添加额外选择器。这些将用于任何将来调用 select ()。若额外选择器列表已改变,调用 select () 将使用新列表,且可能以不同方式返回。
当可以把多个选择器应用于同一文件时,选择第一匹配选择器。选择器的校验次序:
QT_FILE_SELECTORS
环境变量,从左到右
这里是涉及多个选择器的同时匹配范例。它使用平台选择器,加由应用程序基于用户证书设置的名为 admin 的额外选择器。范例有排序以便选取最低匹配文件,若所有选择器存在:
images/background.png images/+linux/background.png images/+windows/background.png images/+admin/background.png images/+admin/+linux/background.png
由于额外选择器的校验,先于平台
+admin/background.png
将在 Windows 被选取当有设置 admin 选择器时,和
+windows/background.png
将在 Windows 被选取当未设置 admin 选择器时。在 Linux,
+admin/+linux/background.png
将被选取当有设置 admin 时,和
+linux/background.png
当不是它时。
创建 QFileSelector instance. This instance will have the same static selectors as other QFileSelector instances, but its own set of extra selectors.
若提供,它将拥有给定 QObject parent .
销毁此选择器实例。
返回用于此实例的完整、有序选择器列表
返回以编程方式被添加到此实例的额外选择器列表。
另请参阅 setExtraSelectors ().
This function returns the selected version of the path, based on the conditions at runtime. If no selectable files are present, returns the original filePath .
If the original file does not exist, the original filePath is returned. This means that you must have a base file to fall back on, you cannot have only files in selectable sub-directories.
See the class overview for the selection algorithm.
This is a convenience version of select operating on QUrl objects. If the scheme is not file or qrc, filePath is returned immediately. Otherwise selection is applied to the path of filePath 和 QUrl is returned with the selected path and other QUrl parts the same as filePath .
See the class overview for the selection algorithm.
设置 list of extra selectors which have been added programmatically to this instance.
These selectors have priority over any which have been automatically picked up.
另请参阅 extraSelectors ().