Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] LocalStorage with custom path & android
Forum Updated to NodeBB v4.3 + New Features

[Solved] LocalStorage with custom path & android

Scheduled Pinned Locked Moved Mobile and Embedded
4 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coursar
    wrote on last edited by
    #1

    Hi to all. I create Qt Quick Application and need to deploy it to Android Device. With application i have Sqlite database with some info which i'm using from LocalStorage in Qt Quick.

    In Desktop i do this stuff (main.cpp):
    @
    int main(int argc, char *argv[])
    {
    QGuiApplication app(argc, argv);

    QtQuick2ApplicationViewer viewer;
    viewer.setLocalStoragePath(QStringLiteral("qml/Project"));
    viewer.setMainQmlFile(QStringLiteral("qml/Project/main.qml"));
    viewer.showExpanded();
    
    return app.exec();
    

    }
    @

    And for qtquick2applicationviewer.cpp
    @
    void QtQuick2ApplicationViewer::setLocalStoragePath(const QString &path)
    {
    engine()->setOfflineStoragePath(QtQuick2ApplicationViewerPrivate::adjustPath(path));
    }
    @

    And in qml/Project i have Database subdir with files:
    c32516babc5b6c47eb8ce1bfc223253c.ini
    c32516babc5b6c47eb8ce1bfc223253c.sqlite

    Everything works fine on Desktop, but on Android i couldn't connect to this database. Any ideas?

    Thanks.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      Is the path present on the filesystem ? Since as per docs,
      "Note that the path may not currently exist on the filesystem, so callers wanting to create new files at this location should create it first - see QDir::mkpath()."
      See "offlineStoragePath":http://qt-project.org/doc/qt-5.0/qtqml/qqmlengine.html#offlineStoragePath-prop

      157

      1 Reply Last reply
      0
      • C Offline
        C Offline
        coursar
        wrote on last edited by
        #3

        I slightly modified code (database & ini file are presented in "assets:" files system. Path: "assets:/qml/Project/databases").

        So:
        @
        void QtQuick2ApplicationViewer::setLocalStoragePath(const QString &path)
        {
        engine()->setOfflineStoragePath(QString("assets:/") + QtQuick2ApplicationViewerPrivate::adjustPath(path));
        }
        @

        But db didn't opened, when i try to open it from js. It just creates the new one.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          coursar
          wrote on last edited by
          #4

          Solved. Assets file system seems to be readonly.

          So:
          @
          #if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_NO_SDK)
          QString dbPath = QString("assets:/") + QtQuick2ApplicationViewerPrivate::adjustPath(path);
          QString hash = QString("c32516babc5b6c47eb8ce1bfc223253c");
          QDir dir(QString("."));
          dir.mkpath(QString("./Databases"));
          QFile dbFile(dbPath + QString("/Databases/") + hash + QString(".sqlite"));
          if (dbFile.exists()) {
          QString newDbName = QString("./Databases/") + hash + QString(".sqlite");
          dbFile.copy(newDbName);
          QFile::setPermissions(newDbName, QFile::WriteOwner | QFile::ReadOwner);
          }
          QFile iniFile(QString("./Databases/") + hash + QString(".ini"));
          iniFile.open(QIODevice::WriteOnly);
          iniFile.write("[General]\nDescription=Catalog\nDriver=QSQLITE\nName=Catalog\nVersion=1.0");
          iniFile.close();
          engine()->setOfflineStoragePath(QLatin1String("."));
          #else
          engine()->setOfflineStoragePath(QtQuick2ApplicationViewerPrivate::adjustPath(path));
          #endif
          @

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved