QStandardPaths::standardLocations on iOs returns path that doesn't exist - intended behavior?
-
Hi,
I am using the following code snippet on iOs to open a sqlite3 database:
SqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); QString path = QStandardPaths::standardLocations(QStandardPaths::DataLocation)[0]; db.setDatabaseName(path + "/mydb.db"); if(!db.open()){ qDebug() << "Couldn't open database: " << db.lastError(); }
This always failed with the following error:
QSqlError(-1, "Error opening database", "out of memory")
It took me a long time to realize that it's because the
QStandardPaths::standardLocations
call returns a path that doesn't exist (yet).The following path gets returned in my case:
/var/mobile/Containers/Data/Application/7E9912071-7A54-48A2-B166-51F5812160A67/Library/Application Support/myapp/myapp
I verified that the path doesn't exist by calling
dir.exists();
on the above path. After executingdir.mkpath(path);
everything worked.My question is now: Is this really the intended behavior? I would have expected that
QStandardPaths
returns a path that's already valid and that I can immediatelly use. -
Hi,
When looking in the docs we can read this one is deprecated.
QStandardPaths::DataLocation Returns the same value as AppLocalDataLocation. This enumeration value is deprecated. Using AppDataLocation is preferable since on Windows, the roaming path is recommended.
Could you try using 'AppDataLocation'
Returns a directory location where persistent application data can be stored. This is an application-specific directory. To obtain a path to store data to be shared with other applications, use QStandardPaths::GenericDataLocation. The returned path is never empty. On the Windows operating system, this returns the roaming path. This enum value was added in Qt 5.4.
It is mentioned this one is never empty. ;-)
Eddy
-
// Android: AppDataLocation works out of the box, iOS you must create the DIR first !! // Android: HomeLocationalso works, iOS: not writable mDataRoot = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0); // guarantee that dirs exist bool ok = checkDirs(); // here I create all missing directories
I'm always using QStandardPaths::AppDataLocation
@Eddy is right: you can also use GenericDataLocation, but I always want to hide my data for other apps -
@ekkescorner If I got you right, then you are also creating the directory first on iOs, right?
-
@Schluchti said in QStandardPaths::standardLocations on iOs returns path that doesn't exist - intended behavior?:
@ekkescorner If I got you right, then you are also creating the directory first on iOs, right?
YEP