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. how to copy file to assets?
Forum Updated to NodeBB v4.3 + New Features

how to copy file to assets?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
5 Posts 3 Posters 951 Views
  • 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.
  • DrageFabeldyrD Offline
    DrageFabeldyrD Offline
    DrageFabeldyr
    wrote on last edited by
    #1

    my program works with database

    first i've tried

        QString tmpString = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
        QFileInfo databaseFileInfo(QString("%1/%2").arg(tmpString).arg("DATA.DB"));
        QString  name = databaseFileInfo.absoluteFilePath();
    

    so i've copied my database from "assets" folder on PC to "name" or created it
    as a result i could read database but couldn't edit
    and if database was created it was empty with no chance to add any record

    now i have

        QString name = "assets:/DATA.DB";
    

    in this case i can create and edit my database on my phone but how to copy existing database to this folder or update it with new version?

        QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile);
    
        QString name = "assets:/DATA.DB";
    
        QFileInfo databaseFileInfo(name);
        if (databaseFileInfo.exists())
        {
            QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok);
        }
        QFileInfo newdatabaseFileInfo(new_name);
        if (newdatabaseFileInfo.exists())
        {
            QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok);
            bool copySuccess = QFile::copy(new_name, name);
            if (!copySuccess)
            {
                QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok);
                name.clear();
            }
            else
            {
                QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok);
            }
        }
    

    how to get success in copying?

    M 1 Reply Last reply
    0
    • DrageFabeldyrD DrageFabeldyr

      my program works with database

      first i've tried

          QString tmpString = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
          QFileInfo databaseFileInfo(QString("%1/%2").arg(tmpString).arg("DATA.DB"));
          QString  name = databaseFileInfo.absoluteFilePath();
      

      so i've copied my database from "assets" folder on PC to "name" or created it
      as a result i could read database but couldn't edit
      and if database was created it was empty with no chance to add any record

      now i have

          QString name = "assets:/DATA.DB";
      

      in this case i can create and edit my database on my phone but how to copy existing database to this folder or update it with new version?

          QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile);
      
          QString name = "assets:/DATA.DB";
      
          QFileInfo databaseFileInfo(name);
          if (databaseFileInfo.exists())
          {
              QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok);
          }
          QFileInfo newdatabaseFileInfo(new_name);
          if (newdatabaseFileInfo.exists())
          {
              QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok);
              bool copySuccess = QFile::copy(new_name, name);
              if (!copySuccess)
              {
                  QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok);
                  name.clear();
              }
              else
              {
                  QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok);
              }
          }
      

      how to get success in copying?

      M Offline
      M Offline
      mvuori
      wrote on last edited by
      #2

      Just a thought: Even though you manage to edit the DB in assets, I don't think you should, as everything under assets is supposed to be read only. And any backup routines wouldn't access the files.
      The problem with the DB copied somewhere else might be in file level permissions. I would check those and change if needed with QFile::setPermissions(). After all, you should be able to do anything with files that are in writeable locations.

      DrageFabeldyrD 1 Reply Last reply
      1
      • M mvuori

        Just a thought: Even though you manage to edit the DB in assets, I don't think you should, as everything under assets is supposed to be read only. And any backup routines wouldn't access the files.
        The problem with the DB copied somewhere else might be in file level permissions. I would check those and change if needed with QFile::setPermissions(). After all, you should be able to do anything with files that are in writeable locations.

        DrageFabeldyrD Offline
        DrageFabeldyrD Offline
        DrageFabeldyr
        wrote on last edited by DrageFabeldyr
        #3

        @mvuori now i can edit my database and rewrite if i have a new one
        but only ONCE
        second time i run my program i have empty database

            QString folder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
            QString  name = folder + "/DATA.DB";
            QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile);
        
            QFile(name).setPermissions(QFileDevice::WriteUser);
        
            QFileInfo databaseFileInfo(name);
            if (databaseFileInfo.exists())
            {
                QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok);
            }
            QFileInfo newdatabaseFileInfo(new_name);
            if (newdatabaseFileInfo.exists())
            {
                QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok);
                bool copySuccess = QFile::copy(new_name, name);
                if (!copySuccess)
                {
                    QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok);
                    //name.clear();
                }
                else
                {
                    QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok);
                }
            }
        
        J.HilkJ 1 Reply Last reply
        0
        • DrageFabeldyrD DrageFabeldyr

          @mvuori now i can edit my database and rewrite if i have a new one
          but only ONCE
          second time i run my program i have empty database

              QString folder = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
              QString  name = folder + "/DATA.DB";
              QString new_name = QStandardPaths::locate(QStandardPaths::DownloadLocation, "DATA.DB", QStandardPaths::LocateFile);
          
              QFile(name).setPermissions(QFileDevice::WriteUser);
          
              QFileInfo databaseFileInfo(name);
              if (databaseFileInfo.exists())
              {
                  QMessageBox::critical(0, QString("Yeah:"), QString("Database exists at %1").arg(name), QMessageBox::Ok);
              }
              QFileInfo newdatabaseFileInfo(new_name);
              if (newdatabaseFileInfo.exists())
              {
                  QMessageBox::critical(0, QString("Attention!"), QString("New database at %1").arg(new_name), QMessageBox::Ok);
                  bool copySuccess = QFile::copy(new_name, name);
                  if (!copySuccess)
                  {
                      QMessageBox::critical(0, QString("Error!"), "Database wasn't updated", QMessageBox::Ok);
                      //name.clear();
                  }
                  else
                  {
                      QMessageBox::critical(0, QString("Yeah!"), "Database was updated", QMessageBox::Ok);
                  }
              }
          
          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @DrageFabeldyr as this is in the Mobile-section of the forum, my guess is, you target Ios&Android

          In that case; i'm not sure, if QStandardPath::HomeLocation is the ideal folder to use for storing application owned files.

          I would suggest QStandardPaths::AppDataLocation , thats the one I use for read/write access of permanent files.
          Keep in mind, that on ios, you'll have to create the folder, as it does not exist by default.

          QDir().mkpath(QStandardPaths::AppDataLocation)
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          DrageFabeldyrD 1 Reply Last reply
          1
          • J.HilkJ J.Hilk

            @DrageFabeldyr as this is in the Mobile-section of the forum, my guess is, you target Ios&Android

            In that case; i'm not sure, if QStandardPath::HomeLocation is the ideal folder to use for storing application owned files.

            I would suggest QStandardPaths::AppDataLocation , thats the one I use for read/write access of permanent files.
            Keep in mind, that on ios, you'll have to create the folder, as it does not exist by default.

            QDir().mkpath(QStandardPaths::AppDataLocation)
            
            DrageFabeldyrD Offline
            DrageFabeldyrD Offline
            DrageFabeldyr
            wrote on last edited by
            #5

            @J.Hilk my target is Android
            thanks for the advice, may be this location is better but it works the same way, nothing changed

            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