0

I have a .txt file in QT project, where I store settings parameters for my app. If I read it that way

void MainWindow::ReadApplicationSettings()
{
   QFile cfgFile("config.txt");
   if(!cfgFile.exists())
   {
      qDebug() << "Cannot find config file.";
   }
   else
   {
      ParseConfigFile(cfgFile);
      SetCfg();
   }
   cfgFile.close();
}

And Parse it:

void MainWindow::ParseConfigFile(QFile &cfgFile)
{
    QString line;
    if (cfgFile.open(QIODevice::ReadOnly | QIODevice::Text))
    {
       QTextStream stream(&cfgFile);
       while (!stream.atEnd())
       {
          line = stream.readLine();
          std::istringstream iss(line.toStdString());
          std::string id, eq, val;
          bool error = false;

          if (!(iss >> id))
          {
            error = true;
          }  
          else if (id[0] == '#')
          {
            continue;
          }
          else if (!(iss >> eq >> val >> std::ws) || eq != "=" || iss.get() != EOF)
          {
            error = true;
          }
          if (error) { throw std::runtime_error("Parse error"); }

          cfgMap[id] = std::stoi(val);
     }
   }
}

The file exists and when the parsing begin the content of the file is empty

The result of: line = stream.readLine(); is "".

If I add the file like a resource file and opened this way:

QFile cfgFile(":config.txt");

It's working correct, but the problem is that the config file is compiled and when you have to change some value, the project must be rebuild to take effect

I tried to compose the path like that QDir::currentPath + "/config.txt", but not working as well.

Rosen Karadinev
  • 205
  • 1
  • 12
  • 2
    Your code will work if you store `config.txt` file in the current working directory - the directory where you run your application from. Or store the file in the same directory where your application is and refer to it as `QCoreApplication::applicationDirPath() + "/config.txt"` Currently you embed it (the content) into the executable using Qt resource framework. – vahancho Oct 23 '19 at 13:24
  • Okey It wokred, but I had to copy manually config.txt to build directory. Thanks :) – Rosen Karadinev Oct 23 '19 at 13:45

2 Answers2

2

Another option is the undocumented feature "file_copies" in Qt 5.6 that you can use like this:

CONFIG += file_copies                                                                                                                                                              
configfiles.path = $$OUT_PWD                                                                                                                                                         
configfiles.files = $$PWD/config.txt                                                                                                                                              
COPIES += configfiles

Found here: https://stackoverflow.com/a/54162789/6842395 If you don't like this method, that post has some more options to choose.

By the way, loooking at your ParseConfigFile() method seems that your config.txt is a collection of lines with the format: key = value, very similar to a classic INI file. Maybe you could use QSettings third constructor like this:

QSettings settings("config.txt", QSettings::IniFormat);
Former contributor
  • 2,466
  • 2
  • 10
  • 15
1

You can use QMAKE_EXTRA_TARGET in your profile like:

copyfile.commands += $${QMAKE_COPY} $$system_path($$PWD/config.txt) $$system_path($$DESTDIR/)
first.depends = $(first) copyfile
QMAKE_EXTRA_TARGETS += first copyfile

But ensure your $$DESTDIR is correct.

Paolo
  • 104
  • 4