PDA

View Full Version : using QSettings to read an ini file



gye325
20th July 2011, 14:24
hi all,

i'm using QSettings to read contents inside an ini file and the ini file looks something like this

[WINDOW_23]
Name=randomwindow
Position=0, 204, 203, 0; cmd, x, y, w, b




set.beginGroup(groupname);
QString position=set.value("Position").toString();
set.endGroup(groupname);


by using the code above, i've been able to go into the group (in this case WINDOW_23) and read the Name. but using the same method, i'm not able to read the Position. can someone tell me what I've done wrong?

qt and c++ total amateur here. thanks in advance!

high_flyer
20th July 2011, 14:35
What happens if you removed the spaces?:
Position=0,204,203,0;cmd,x,y,w,b

gye325
20th July 2011, 15:02
What happens if you removed the spaces?:
Position=0,204,203,0;cmd,x,y,w,b

that file can't be edited. I have to read it as it is

high_flyer
20th July 2011, 15:04
It doesn't matter, make a copy, and edit the copy and try that.
If you then can get the value, then at least you know what the problem is, and you know that you either have to subclass QSettings to allow that kind of lines, or do the reading your self.

gye325
20th July 2011, 15:09
It doesn't matter, make a copy, and edit the copy and try that.
If you then can get the value, then at least you know what the problem is, and you know that you either have to subclass QSettings to allow that kind of lines, or do the reading your self.

ah ok. just tried that. still doesn't return anything. i get a return of "", like before. any recommendations?

high_flyer
20th July 2011, 15:19
Post your code.

gye325
20th July 2011, 15:25
QString grpName = "WINDOW_" + QString::number(i+1);
set.beginGroup(grpName);
CnaWindow *win = new CnaWindow();
win->title= set.value("Title").toString();
win->number= set.value("Number").toInt();
win->position= set.value("Position").toString();
set.endGroup();


and here's a part of the ini file

[WINDOW_14]
Title=Balken
Type=2
Comment=Balken-Fenster
Number=14
Position=0, 50, 50, 450, 100 ;cmd, x, y, w, h
DisplayMask=32
JointIndex=4294967295
Grid=1
RelMode=0
VerMode=0
ShowLegend=1
LegendPixWidth=66
YValue=0
ObjectMode=0
AxisWidth=0
AxisHeight=0
SigDispMode=2
LegendPos=2
LegendHeight=0
LegendScrollPos=0
AxisScrollPos=6594188
ShowSignalComments=1
MaximizeToPage=0, 0, 0, 0 ;x, y, w, h

high_flyer
20th July 2011, 15:53
Hmm...
What does "contains" return:


QString grpName = "WINDOW_" + QString::number(i+1);
set.beginGroup(grpName);
CnaWindow *win = new CnaWindow();
win->title= set.value("Title").toString();
bool bContains = set.contains("Number");
win->number= set.value("Number").toInt();
bContains = set.contains("Position");
win->position= set.value("Position").toString();
set.endGroup();


And, do you get a string if you change the value of "Position" to some normal string:
Position=test

Also, try this:

QStringList keys = set.childKeys();

gye325
20th July 2011, 16:07
'contains' will only check if that string is contained in that group

yeap, i do get the normal string. i'm just wondering why something like '0, 0, 2, 4' can't be treated like a normal string. even without the spaces in between

and 'set.childKeys' basically returns 'Title', 'Type', 'Comment', all the strings that appear before the '=' in each line

high_flyer
20th July 2011, 16:24
'contains' will only check if that string is contained in that group
I know. But I wanted to make sure that "Position" is seen as part of the group.


i'm just wondering why something like '0, 0, 2, 4' can't be treated like a normal string. even without the spaces in between
I don't know, to answer that you can look in the QSettings code.
My guess is, that QSettings will read a line until it encounters some sort of what it considers to be an end of line, maybe ';' or maybe it only looks for alpha numerics, so the next ',' stops it...
At least you know what the problem is, that usually is half of the solution.
Now you can subclass QSettings, and adjust it to handle such lines too.

gye325
20th July 2011, 16:37
My guess is, that QSettings will read a line until it encounters some sort of what it considers to be an end of line, maybe ';' or maybe it only looks for alpha numerics, so the next ',' stops it...
At least you know what the problem is, that usually is half of the solution.
Now you can subclass QSettings, and adjust it to handle such lines too.

how do i adjust QSettings to handle such lines too?

high_flyer
20th July 2011, 16:41
Amm... you code it?
Or I don't understand your question.
Look in to QSettings to see what it does when you call value().
Step through, and see what makes it return and not read the line.
In your subclass then, introduce code that will allow dealing with such lines as the one you have.

Another options is to not use QSettings at all, but write your own class for it.
Since you have rather a simple case (each key is the start of a line, and ends with '=', and value is everything until the end of the line) this should be rather easy to implement.

mcosta
20th July 2011, 16:47
I tried with several string values,

when the string contains comma ',', semicolon ';' or equal '=' character, you have to "quote" the string

this

Position="0, 50, 50, 450, 100 ;cmd, x, y, w, h"

works

high_flyer
20th July 2011, 17:06
Yes, but as he said, he can't change the ini file...
He will have to change the way QSettings deals with ',' ';' and '=' mamely, only stop on \r\n.

mcosta
20th July 2011, 17:38
Probably he have to use QSettings::registerFormat() in order to create a "special" INI parser.

QSettings has not virtual methods or properties to access the INI parser

high_flyer
20th July 2011, 17:41
Probably he have to use QSettings::registerFormat() in order to create a "special" INI parser
Yes, this looks like a good way to go.

danielweberdlc
3rd September 2011, 00:07
It looks like values that are separated by "," are returned as a QStringList, and not a QString by QSettings.

Cast the return value to a QStringList and manipulate the value that way.