Pergunta

I'm writing cross-platform mobile application using Qt/QML. The app installs along with a small sqlite database that at start copied from assets to application data location and than should be accessed by the app. For this purpose I have DatabaseManager class. It contains function that provide path to assets on different platforms:

QString DatabaseManager::assetsPath()
{
    QString assetsPath;
#ifdef Q_OS_WIN
    assetsPath = QDir::toNativeSeparators("assets:/../database");
#endif

#ifdef Q_OS_ANDROID
    assetsPath = QDir::toNativeSeparators("assets:/database");
#endif

#ifdef Q_OS_IOS
    assetsPath = QDir::toNativeSeparators("assets:/database");
#endif

#ifdef Q_OS_MAC
    assetsPath = QDir::toNativeSeparators("assets:/database");
#endif

    return assetsPath;

}

Assets deployed in .pro file with following code:

folder_qml.source = qml/HookahMixes
folder_qml.target = qml

folder_db.source = database
folder_db.target = .

DEPLOYMENTFOLDERS = folder_qml folder_db

The problem is that "assets:/" prefix works well on Windows and Android but doesn't work for ios and Mac OS. So the question is: what path to assets should I use to find database on that platforms? Thanks.

Foi útil?

Solução

Ok, so its time to answer own question. The approach I used was incorrect and the problem can be solved much easier:

Qt generates QtQuick2ApplicationViewer class to handle main qml loading. Inside this class you can find a private function "adjustPath()". This function do exactly what we need - maps your local path to resources to global platform-dependent path. In order to use the function I maded it publicly visible and use it in form:

QString DatabaseManager::assetsPath()
{
    return m_Viewer.adjustPath("database");
}

Thats it.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top