Quantcast
Channel: Active questions tagged config - Stack Overflow
Viewing all articles
Browse latest Browse all 5049

How to add non compilable configuration file into QT Project?

$
0
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.


Viewing all articles
Browse latest Browse all 5049

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>