PDA

View Full Version : QSettings



incapacitant
24th May 2006, 17:56
I am using :


settings.value( "coco", "ERROR" ).toString();

this works



const QString s = "coco";
settings.value( s, "ERROR" ).toString();

this insists on returning "ERROR"

Is there a way to pass a variable to settings.value ?

s being a const string

gfunk
24th May 2006, 19:00
You can't change the value of const variables; that's why the compiler won't let you do:
s = "asdf";

But you can just remove the const for your s variable, since you always can pass non-const variables into functions that take const variables. The const just means that the function won't (and shouldn't) change the value of the variable passed in.

QString s = "coco";
s = "asdf";
settings.value( s, "ERROR" ).toString();

Is this what you are asking?

incapacitant
24th May 2006, 19:15
QString x = "TxtCharSMSMax";
qS = settings.value( x, "ERROR" ).toString();


Despite the fact that "TxtCharSMSMax" is a valid key, settings.value does not accept it.
It seems it takes only settings.value("something") and nothing else than quoted values.

Can't find a way to pass a value.

gfunk
24th May 2006, 19:50
Weird. What's the exact text message of the error message from the compiler? Are you using Visual C++ or MinGW?

michael
24th May 2006, 20:02
I am using :


settings.value( "coco", "ERROR" ).toString();

this works



const QString s = "coco";
settings.value( s, "ERROR" ).toString();

this insists on returning "ERROR"

Is there a way to pass a variable to settings.value ?

s being a const string


const QString works for me on (Qt 4.1.3 / WindowsXP / Visual Studio 2005) and (Qt 4.0.1 / WindowsXP / mingw - g++)

Here is the code:


#include <QString>
#include <QSettings>
#include <iostream>

int main(int argc, char *argv[])
{
QSettings settings("TestSoft", "QSettings Test");
settings.setValue("testName", 100);
const QString s = "testName";
std::cout << "Test call with char string: "
<< settings.value("testName", "ERROR").toString().toStdString() << std::endl
<< "Test call with QString: "
<< settings.value(s, "ERROR").toString().toStdString() << std::endl
<< "For good measure create error: "
<< settings.value("doesNotExist", "ERROR").toString().toStdString() << std::endl;
char t;
std::cin >> t;
return 0;
}


This is the output:

C:\...>qsettings.exe
Test call with char string: 100
Test call with QString: 100
For good measure create error: ERROR

exit

C:\...>

I hope this helps you. If you want to compile the code above don't forget to add the following to the *.pro file as this is a console application.
CONFIG += console

-Michael

gfunk
24th May 2006, 20:12
So what is the text of the problem when you try to use "QString s" instead of "const QString s"?

wysota
24th May 2006, 21:00
I think there is no compiler error, just the key is not found in the ini file.

Maybe you're missing some beginGroup("...") statement?

incapacitant
24th May 2006, 22:37
Ok, it works so thanks to everybody but I am not sure I solve it the best way.

QSettings is set there at the very top of the application:


ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 )
{
QCoreApplication::setOrganizationName("Adrien");
QCoreApplication::setApplicationName("EFREI SMS");
QSettings settings("efrei.ini", QSettings::IniFormat);


I had to put it public in this class AND pass it as parameter :


qS = iniGet( settings, "TxtCharSMSMax" ) + " " + qS2;


to this procedure :


QString ApplicationWindow::iniGet( QSettings &settings, const QString &s )
{
QString parameter;
QString qS3;
QString qS4;
parameter = settings.value( s, "ERROR" ).toString();
if ( parameter == "ERROR" )
{
qS3 = "Unable to load parameter from efrei.ini";
qS4 = s + " not found. Replaced with 'Error'";
QMessageBox::critical( this, qS3, qS4 );
parameter = "Error";
}
return parameter;
}


When QSettings settings was declared private is this same class, for some reason it was not recognized in the iniGet.

wysota
24th May 2006, 22:56
You probably didn't initialize it properly.

incapacitant
25th May 2006, 07:53
What do you mean initialize. I wrote :


class ApplicationWindow: public QMainWindow
{
Q_OBJECT

private:
QSettings settings;


What else is needed.

It all looks like settings is a local variable to ApplicationWindow.

wysota
25th May 2006, 09:43
Compare those two:


QSettings settings("efrei.ini", QSettings::IniFormat);
QSettings settings;

incapacitant
25th May 2006, 10:32
QSettings is initialised here :


ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 )
{
QCoreApplication::setOrganizationName("Adrien");
QCoreApplication::setApplicationName("EFREI SMS");
QSettings settings("efrei.ini", QSettings::IniFormat);



If I declare in private :
QSettings settings("efrei.ini", QSettings::IniFormat);
instead of
QSettings settings;
the compiler does not accept it.

wysota
25th May 2006, 10:50
QSettings is initialised here :


ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 )
{
QCoreApplication::setOrganizationName("Adrien");
QCoreApplication::setApplicationName("EFREI SMS");
QSettings settings("efrei.ini", QSettings::IniFormat);

This is a local variable, not the member of the class.



If I declare in private :
QSettings settings("efrei.ini", QSettings::IniFormat);
instead of
QSettings settings;
the compiler does not accept it.
Of course, this would be invalid.
Try this:


QCoreApplication::setOrganizationName("Adrien"); // you should put that in main.cpp
QCoreApplication::setApplicationName("EFREI SMS"); // and this too
//...
ApplicationWindow::ApplicationWindow()
: QMainWindow( 0 ), settings("efrei.ini", QSettings::IniFormat)
{

incapacitant
25th May 2006, 11:08
It works and I meet you I will ask for a brain transplant.