QgsFileUtils class

Class for file utilities.

Contents

Public static functions

static auto addExtensionFromFilter(const QString& fileName, const QString& filter) -> QString
Ensures that a fileName ends with an extension from the specified filter string.
static auto ensureFileNameHasExtension(const QString& fileName, const QStringList& extensions) -> QString
Ensures that a fileName ends with an extension from the provided list of extensions.
static auto extensionsFromFilter(const QString& filter) -> QStringList
Returns a list of the extensions contained within a file filter string.
static auto findClosestExistingPath(const QString& path) -> QString
Returns the top-most existing folder from path.
static auto representFileSize(qint64 bytes) -> QString
Returns the human size from bytes.
static auto stringToSafeFilename(const QString& string) -> QString
Converts a string to a safe filename, replacing characters which are not safe for filenames with an '_' character.

Function documentation

static QString QgsFileUtils::addExtensionFromFilter(const QString& fileName, const QString& filter)

Ensures that a fileName ends with an extension from the specified filter string.

E.g. a fileName of "d:/my_file" with a filter of "GeoTIFF Files (*.tiff *.tif)" will return "d:/my_file.tif", where as fileName of "d:/my_file.TIFF" or "d:/my_file.TIF" will be returned unchanged.

static QString QgsFileUtils::ensureFileNameHasExtension(const QString& fileName, const QStringList& extensions)

Ensures that a fileName ends with an extension from the provided list of extensions.

E.g. if extensions contains "tif" and "tiff", then a fileName of "d:/my_file" will return "d:/my_file.tif". A fileName of "d:/my_file.TIFF" or "d:/my_file.TIF" will be returned unchanged.

static QStringList QgsFileUtils::extensionsFromFilter(const QString& filter)

Returns a list of the extensions contained within a file filter string.

E.g. a filter of "GeoTIFF Files (*.tiff *.tif)" would return a list containing "tiff", "tif". The initial '.' is stripped off the extension.

static QString QgsFileUtils::findClosestExistingPath(const QString& path)

Returns the top-most existing folder from path.

E.g. if path is "/home/user/projects/2018/P4343" and "/home/user/projects" exists but no "2018" subfolder exists, then the function will return "/home/user/projects".

static QString QgsFileUtils::stringToSafeFilename(const QString& string)

Converts a string to a safe filename, replacing characters which are not safe for filenames with an '_' character.