QFileSelector Class
The QFileSelector class provides a convenient way of selecting file variants. More…
| Header | #include <QFileSelector> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
| Since | 5.2 |
| Inherits | QObject |
List of all members, including inherited members
Public Functions
| QFileSelector(QObject *parent = nullptr) | |
| ~QFileSelector() | |
| QString | select(const QString &filePath) const |
| QUrl | select(const QUrl &filePath) const |
| QStringList | extraSelectors() const |
| void | setExtraSelectors(const QStringList &list) |
| QStringList | allSelectors() const |
Detailed Description
A program can ship several variants of a file, for a platform or a language, in directories whose names start with a plus sign, next to the file they replace:
images/background.png
images/+linux/background.png
images/+de_DE/background.png
select() returns the variant that fits the selectors that are active, or the path it was given if no variant does:
QFileSelector selector;
const QString path = selector.select(u"images/background.png"_s); // images/+linux/background.png
The selectors, in the order they are tried, are the extra selectors that the program set with setExtraSelectors(), the comma-separated names in the environment variable QT_FILE_SELECTORS, the name of the default locale such as de_DE, and the names of the platform: unix, the kernel such as linux, and the distribution such as ubuntu. Selector directories nest, and a variant in +linux/+de_DE is preferred to the one in +linux. The file must also exist outside the selector directories.
Files in resources are selected in the same way: give select() a path that starts with :/ or a URL with the scheme qrc.
See also QLocale and QSysInfo.
Differences from Qt
The environment variable is read by every call of allSelectors(), not once for the process. There is no assets: scheme, which only Android has.
Member Function Documentation
QFileSelector::QFileSelector(QObject *parent = nullptr)
Constructs a file selector with the given parent and no extra selectors.
QFileSelector::~QFileSelector()
Destroys the file selector.
QStringList QFileSelector::allSelectors() const
Returns every selector that select() uses, the one with the highest priority first: the extra selectors, those of the environment variable QT_FILE_SELECTORS, the name of the default locale and the platform names.
QStringList QFileSelector::extraSelectors() const
Returns the selectors that the program added with setExtraSelectors().
QString QFileSelector::select(const QString &filePath) const
Returns the variant of filePath that fits the active selectors best, or filePath if it has no fitting variant or does not exist.
See also allSelectors().
QUrl QFileSelector::select(const QUrl &filePath) const
This is an overloaded function.
Returns the URL of the variant of filePath. Only URLs of local files and of resources, with the scheme file or qrc, have variants; any other URL is returned unchanged.
void QFileSelector::setExtraSelectors(const QStringList &list)
Sets the selectors of the program to list. They are tried before all others, the first one first.
See also extraSelectors() and allSelectors().