PDA

View Full Version : How to read .ini file from resources files with QSettings?



orkto
21st December 2014, 00:57
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSettings>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSettings * qsettings = new QSettings(":/config.ini",QSettings::IniFormat);
bool status = qsettings->value("preview","").toBool();
qDebug() << status;

}

MainWindow::~MainWindow()
{
delete ui;
}
http://i.imgur.com/XKOBC7o.png
Once i could do it but now i don't know whats wrong. When i googled this problem i just saw that this impossible but i enshure that i did it before.

anbu01
21st December 2014, 07:05
HI,
Try this it will help you to retrieve all values from file and then you can iterate to get the particular value.



QSettings settings("config.ini", QSettings::IniFormat);
settings.beginGroup("General");
QStringList keys = settings.allKeys();

Radek
21st December 2014, 09:45
The second parameter of value() is a default value if the key (the first parameter) has not been found. Therefore:


qsettings->beginGroup("General");
bool status = qsettings->value("preview",false).toBool();
qsettings->endGroup();

or


qsettings->beginGroup("General");
bool status = qsettings->value("preview",true).toBool();
qsettings->endGroup();

If you want to check whether you are reading config.ini correctly, use the first variant.

orkto
21st December 2014, 17:28
Thx guys. The problem is Qt creator don't include resource file until launch qmake. Strange behavior.

d_stranz
21st December 2014, 22:13
Why would you want to store config information in the app's resources? This means they can't be changed at run time, because that would require rebuilding the executable. And since you have to run the resource compiler and relink the executable every time the resources change, why bother using ini format at all?

wysota
23rd December 2014, 07:50
Thx guys. The problem is Qt creator don't include resource file until launch qmake. Strange behavior.

You probably mean that if you modify the ini file already added to the resource file that doesn't cause the resource file to be updated with the new content. I'd say this is normal and expected, I wouldn't expect make to read and parse my resource files upon every invocation. That would dramatically increase compilation time.

d_stranz
24th December 2014, 18:42
I'd say this is normal and expected, I wouldn't expect make to read and parse my resource files upon every invocation.

Visual Studio appears to take a different approach. If I edit a file included as a resource (like an icon, for example), the resource file (qrc) is recompiled next time around.

wysota
24th December 2014, 18:48
Visual Studio appears to take a different approach. If I edit a file included as a resource (like an icon, for example), the resource file (qrc) is recompiled next time around.

Visual Studio doesn't use qmake so it has no other choice.

anda_skoa
24th December 2014, 21:07
You probably mean that if you modify the ini file already added to the resource file that doesn't cause the resource file to be updated with the new content. I'd say this is normal and expected, I wouldn't expect make to read and parse my resource files upon every invocation. That would dramatically increase compilation time.

Hmm.
I haven't used this lately, but I vaguely remember that in a project which had QML files in a resource any change made to a QML file did make it into the next build.

Cheers,
_

wysota
25th December 2014, 09:09
Hmm.
I haven't used this lately, but I vaguely remember that in a project which had QML files in a resource any change made to a QML file did make it into the next build.

I remember it worked in some conditions but not in others. Haven't tried that lately, especially without Qt Creator. Make would have to have a dependency set on the contents of the qrc file.

anda_skoa
25th December 2014, 10:53
I remember it worked in some conditions but not in others. Haven't tried that lately, especially without Qt Creator. Make would have to have a dependency set on the contents of the qrc file.

Right.
I think it works if the files are specified explicitly and does not if specified by wildcards.

Cheers,
_

wysota
26th December 2014, 12:02
It could be that Creator touches the qrc file whenever it saves a qml file belonging to the resource. This would cause make to rebuild it.