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. Reliable way to read a file in the project directory
Forum Updated to NodeBB v4.3 + New Features

Reliable way to read a file in the project directory

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 256 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.
  • G Offline
    G Offline
    grinqt
    wrote on last edited by
    #1

    Hey Everyone!

    I'm trying to read a file in called "CQF.json" in the "data/" directory of my project. "data/CQF.json" is in my resourses.qrc file. I've found that setting my current directory to "../.." exits the build/<SPECIFIC_BUILD> directory and puts me in the directory of my project, allowing me to open the file I want, like so:

        QDir::setCurrent("../..");
        QFile file("data/CQF.json");
        bool success = file.open(QIODevice::ReadOnly);
    

    The code above works as far as I can tell, but I'm wondering if there is a better way to do this.

    • Is there a way to set the directory to the base project directory which doesn't involve exiting the build directory?
    • Is there a way to read this file without having to worry about the current directory?
    • Or is the way I presented about the standard method of reading such a data file?

    Any tips / help appreciated!

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Put the file into a Qt resource and read it from there.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      G 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Put the file into a Qt resource and read it from there.

        G Offline
        G Offline
        grinqt
        wrote on last edited by
        #3

        @Christian-Ehrlicher Perfect, thanks!

        I have changed the code to the following:

            QFile file(":/data/CQF.json");
            bool success = file.open(QIODevice::ReadOnly);
        
        1 Reply Last reply
        1
        • G grinqt has marked this topic as solved on

        • Login

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