Unsolved Having issues with QSettings and an existing INI file,
-
Hello,
I am trying to use qSettings to store an ini file with some key:value pairs to access later on.
Sample INI data:
[CONFFILES] APPLICATION_START_VECTOR_LOCATION=100 BOOTLOADER_END_ADDR=1400 BOOTLOADER_START_ADDR=400 FILE_FORMAT_TYPE=223D FILE_FORMAT_VERSION=1
my code:
QSettings settings("FirmwareConf.ini", QSettings::IniFormat); settings.beginGroup("CONFFILES"); const QStringList childKeys = settings.childKeys(); QStringList values; foreach (const QString &childKey, childKeys) values << settings.value(childKey).toString(); settings.endGroup(); qDebug() << "all values" << values;
the qDebug is just outputting all values (), where I thought i would be seeing my key:values from the ini file.
-
Hi and welcome to the forums
Are you sure it finds the file?
-
Hi and welcome to devnet,
The path you use for QSettings is a relative path. So your application will try to open it at the same place the executable can be found. This is likely not what you want.
Where is that file exactly located on your computer ?
-
the ini is in the Other files folder of the Project
-
That's a virtual view, not a reflection of your filesystem.
Based on that, I guess that the file is in the root folder of your project, correct ?
-
that is correct
-
Then use the full path to the file if you want it to be properly loaded.
-
@SGaist Thanks that did the trick!
How would this effect the exe if it's sent to another system, does the INI file get included in the built exe?
-
Hi
Just as a note:
if you later deploy the app , using a fixed path is often not optimal.
There is also the option to use
http://doc.qt.io/qt-5/qstandardpaths.htmland simply put the file there in one of the locations to
ensure it works on other system.ps. did you mean to embed the ini file into the exe ?
-
@mrjj i'm very new to QT, let alone c++ so i am not very familiar with best/proper practices. I want the ini to be accessible if the executable gets distributed, what's best practice for that?
-
Since you are likely going to read and write that ini file, one technique is to have one with default values embedded in your application using Qt's resource system. Then at the start of your application, copy it over the correct location using QStandardPaths and then you can access the file from your application.
-
Hi
To implement what @SGaist suggest you would do the following:
1:
Make sure the file is added to the resource file
Right click the .qrc in project view and select Add existing file
Make sure it lo0ks something like in sample and not listed under "other files."
2:
copy it to location when run. I used document folder. there are others if u wish.#include "mainwindow.h" #include <QApplication> #include <QSettings> #include <QDebug> #include <qstandardpaths.h> #include <QFile> int main(int argc, char* argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); const QString IniFileName("FirmwareConf.ini"); // ask where doc folder is QString DocFolder = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation); // create a new string with full path and filename QString InfilePathAndFileName = DocFolder + "/" + IniFileName; // copy file from res file to writable location. the ":/" is due to it being inside qres file. QFile::copy(":/" + IniFileName, InfilePathAndFileName ); // output in Creators message pane where we copy. ( so we can debug if doesnt work) qDebug() << "Copying inifile to " << InfilePathAndFileName; // load it from new location QSettings settings(InfilePathAndFileName, QSettings::IniFormat); settings.beginGroup("CONFFILES"); const QStringList childKeys = settings.childKeys(); QStringList values; foreach (const QString& childKey, childKeys) { values << settings.value(childKey).toString(); } settings.endGroup(); qDebug() << "all values" << values; return a.exec(); }