Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile on resource file can not be written to
Forum Updated to NodeBB v4.3 + New Features

QFile on resource file can not be written to

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 14.3k 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.
  • S Offline
    S Offline
    steveheflin
    wrote on last edited by
    #1

    I'm unable to open a resource file for write. I can open the same file using its full path just fine, but not when the path is specified as a resource. AND YES, the file is specified in my qrc file and I can open it just file for read.
    @
    QString filename = QString::fromUtf8(":/dbase/myfile.xml");
    QFile file(filename);
    if ( ! file.open(QFile::WriteOnly | QFile::Text))
    file_failed_to_open();
    @
    Then I tried it using the full path and it worked just fine:
    @
    QString filename = QString::fromUtf8("/home/user/proj_root/dbase/myfile.xml");
    QFile file(filename);
    if ( file.open(QFile::WriteOnly | QFile::Text))
    file_opened_as_expected();
    @
    So as a work around, I tried to use the "absolutePath" member of a resource to get the full path/filename. That didn't work either. The error message indicates that the resource path was not modified.
    @
    QString sTemp = QString::fromUtf8(":/dbase/myfile.xml");
    QResource * rTemp = new QResource(sTemp);
    filename = rTemp->absoluteFilePath();
    delete rTemp;
    QFile file(filename);
    if (! file.open(QFile::WriteOnly | QFile::Text))
    file_failed_to_open();
    @

    Can somebody please help me understand what's going on? I'm using QT4.8.2 on a Linux-Fedora-16_x86_64 desktop.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      As far as I know, files that are compiled into the executable using the Qt Resource system are read-only. You can't modify them, so any attempt to open such file for writing is going to fail. How should it be possible to modify resources compiled into the EXE file? Especially an executable can't modify itself while it is running. You'll have to re-compile the QRC file (using RCC) and then re-build the EXE file, if one of the resources has changed.

      Note: You may use QResource to access the data of the resource directly - but again that memory area is read-only. So if you need to modify that data, you'll have to copy it into a your own memory buffer first. Of course this way all modifications will be lost as soon as the application terminates. Write the contents of the memory buffer into to a separate (none-resource) file, if you need to store it durably...

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Peppy
        wrote on last edited by
        #3

        I think resource files compiled into binaries are read-only. In your second example you have absolute path to file on your hard drive, not in your resources. And if I am right, there is no way how to do some work around.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          steveheflin
          wrote on last edited by
          #4

          Thanks for the replies people, I really appreciate it. So I need a way to find my root program location and perform my own run-time construction of the full path/filenames to my XML database files and NOT include the XML files in the resources. Right?

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            [quote author="steveheflin" date="1346371275"]Thanks for the replies people, I really appreciate it. So I need a way to find my root program location and perform my own run-time construction of the full path/filenames to my XML database files and NOT include the XML files in the resources. Right? [/quote]
            AFAIK the resource files must be read only as the others already stated. However, you may use it for initial reading and writing a new xml file somewhere.

            The full path to the application shall be part of the runtime argument. argv[0] should be the name of the application. Typically it contains already the full path to the application. However, there is also a method for "QApplication::applicationDirPath":http://qt-project.org/doc/qt-4.8/qcoreapplication.html#applicationDirPath . It is for sure more secure to use this.

            You might want to think again. It is not a good idea to write things to the place of the executables. Most OS prevent this in the mean time. Typically today's OS have a scheme where to place the application data. You can also use "QSettings":http://qt-project.org/doc/qt-4.8/qsettings.html to store some runtime information.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • T Offline
              T Offline
              twsimpson
              wrote on last edited by
              #6

              "QDesktopServices":http://qt-project.org/doc/qt-4.8/qdesktopservices.html should help you out here, it gives you a way to retrieve the locations where certain kinds of data should be stored or retrieved from, including persistent application storage.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                steveheflin
                wrote on last edited by
                #7

                Hey thank to all, this is all good advice. Keeping my database files under the executable is really not a good idea.

                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