Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a file on different mobile phones in Qt

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.

like image 750
Eray Tuncer Avatar asked Oct 27 '14 21:10

Eray Tuncer


1 Answers

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.

like image 145
BaCaRoZzo Avatar answered Oct 06 '22 17:10

BaCaRoZzo