QFileSelector 類

QFileSelector 為選擇文件變體提供便捷方式。 更多...

頭: #include <QFileSelector>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core
繼承: QObject

公共函數

QFileSelector (QObject * parent = nullptr)
virtual ~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 對於基於平颱或設備特色,選擇文件變體很方便。這允許在某些情況下更輕鬆開發和部署包含所有不同變體的代碼庫,譬如:當在部署步驟期間無法確定正確變體時。

使用 QFileSelector

若始終使用同一文件,則不需要使用 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。

對於不存在有效選擇器的錯誤處理,推薦在基文件位置下存在默認文件或錯誤處理文件,即使期望所有部署都存在選擇器。

在未來版本中,某些可能會被標記為部署時靜態,並在部署步驟期間因優化而移動。由於選擇器自帶性能開銷,推薦避免將它們用於涉及性能關鍵代碼的情況。

添加選擇器

選擇器通常可用於的

  • 平颱,在其中運行應用程序匹配下列字符串的任何平颱 (列錶不詳盡):Android、iOS、OSX、Darwin、Mac、MacOS、Linux、QNX、Unix、Windows。在 Linux,若可以確定,還可以確定分發名稱,像 Debian、Fedora 或 openSUSE。
  • 區域設置,如 QLocale().name()。

進一步,選擇器將添加自 QT_FILE_SELECTORS 環境變量,設置時應該是以逗號分隔的一組選擇器。注意,隻會讀取此變量一次;選擇器可能不更新,若變量在應用程序運行時改變。僅在首次使用時,評估初始選擇器集一次。

還可以在運行時,為自定義行為添加額外選擇器。這些將用於任何將來調用 select ()。若額外選擇器列錶已改變,調用 select () 將使用新列錶,且可能以不同方式返迴。

當應用多個選擇器時的衝突解決方案

當可以把多個選擇器應用於同一文件時,選擇第一匹配選擇器。選擇器的校驗次序:

  1. 選擇器集憑藉 setExtraSelectors (),按它們在列錶中的次序
  2. 選擇器在 QT_FILE_SELECTORS 環境變量,從左到右
  3. 區域設置
  4. 平颱

這裏是涉及多個選擇器的同時匹配範例。它使用平颱選擇器,加由應用程序基於用戶證書設置的名為 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 當不是它時。

成員函數文檔編製

[explicit] QFileSelector:: QFileSelector ( QObject * parent = nullptr)

創建 QFileSelector 實例。此實例將擁有如其它 QFileSelector 實例的相同靜態選擇器,但它有自己的一組額外選擇器。

若提供,它將擁有給定 QObject parent .

[virtual noexcept] QFileSelector:: ~QFileSelector ()

銷毀此選擇器實例。

QStringList QFileSelector:: allSelectors () const

返迴用於此實例的完整、有序選擇器列錶

QStringList QFileSelector:: extraSelectors () const

返迴以編程方式被添加到此實例的額外選擇器列錶。

另請參閱 setExtraSelectors ().

QString QFileSelector:: select (const QString & filePath ) const

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.

QUrl QFileSelector:: select (const QUrl & filePath ) const

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.

void QFileSelector:: setExtraSelectors (const QStringList & list )

設置 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 ().