I want to create a cross-platform application. It does not belong to any particular tablets or smartphones. It is supposed to be run on any device. However, I want to create a file on devices. The problem is that I do not know where to save it. Because it is cross-platform, I cannot specify any path for that file. Any idea how to do such save process independent from any specific platform.
Qt provides the nice class QStandardPaths
. Directly from the docs:
The QStandardPaths class provides methods for accessing standard paths. This class contains functions to query standard locations on the local filesystem, for common tasks such as user-specific directories or system-wide configuration directories.
In particular you should use the static method:
QStringList QStandardPaths::standardLocations(StandardLocation type)
A common usage can be defined for the home directory as follows:
QString basePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation)[0]
Note that I've queried the returned StringList
for the first entry, without checking if it is empty. That's safe since the returned object is never empty for the home location! That's not always the case but the docs are quite detailed about that and also about which locations are returned on each specific platform. Note also that, if not specified differently (see again the docs for specific cases), the first entry returned in the StringList
is always the writable one.
As of Qt 5.5 the documentation is complete for what concerns iOS and Android. That's because on newer Qt versions the iOS file system is correctly and completely supported. That was not true for versions minor than 5.2 (see for instance the closed QTBUG-34199 but also read QTBUG-36171 for more details about iOS QStandardPaths
support throughtout Qt releases cycle).
Finally, an advice. If you are interested in sharing files between an android/iOS device and a PC via USB, a good location choice can be QStandardPaths::DownloadLocation
. Mind that in iOS additional policies for file sharing must be defined, i.e. UIFileSharingEnabled
key must be set in the info plist of your app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With