I am using :
this worksCode:
settings.value( "coco", "ERROR" ).toString();
this insists on returning "ERROR"
Is there a way to pass a variable to settings.value ?
s being a const string
Printable View
I am using :
this worksCode:
settings.value( "coco", "ERROR" ).toString();
this insists on returning "ERROR"
Is there a way to pass a variable to settings.value ?
s being a const string
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?
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.
Weird. What's the exact text message of the error message from the compiler? Are you using Visual C++ or MinGW?
Quote:
Originally Posted by incapacitant
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:
Code:
#include <QString> #include <QSettings> #include <iostream> int main(int argc, char *argv[]) { settings.setValue("testName", 100); 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:
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.Quote:
C:\...>qsettings.exe
Test call with char string: 100
Test call with QString: 100
For good measure create error: ERROR
exit
C:\...>
CONFIG += console
-Michael
So what is the text of the problem when you try to use "QString s" instead of "const QString s"?
I think there is no compiler error, just the key is not found in the ini file.
Maybe you're missing some beginGroup("...") statement?
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:
Code:
ApplicationWindow::ApplicationWindow() {
I had to put it public in this class AND pass it as parameter :
Code:
qS = iniGet( settings, "TxtCharSMSMax" ) + " " + qS2;
to this procedure :
Code:
When QSettings settings was declared private is this same class, for some reason it was not recognized in the iniGet.
You probably didn't initialize it properly.
What do you mean initialize. I wrote :
What else is needed.
It all looks like settings is a local variable to ApplicationWindow.
QSettings is initialised here :
Code:
ApplicationWindow::ApplicationWindow() {
If I declare in private :
QSettings settings("efrei.ini", QSettings::IniFormat);
instead of
QSettings settings;
the compiler does not accept it.
This is a local variable, not the member of the class.Quote:
Originally Posted by incapacitant
Of course, this would be invalid.Quote:
If I declare in private :
QSettings settings("efrei.ini", QSettings::IniFormat);
instead of
QSettings settings;
the compiler does not accept it.
Try this:
Code:
//... ApplicationWindow::ApplicationWindow() {
It works and I meet you I will ask for a brain transplant.