Results 1 to 17 of 17

Thread: using QSettings to read an ini file

  1. #1
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default using QSettings to read an ini file

    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


    Qt Code:
    1. set.beginGroup(groupname);
    2. QString position=set.value("Position").toString();
    3. set.endGroup(groupname);
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    What happens if you removed the spaces?:
    Position=0,204,203,0;cmd,x,y,w,b
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QSettings to read an ini file

    Quote Originally Posted by high_flyer View Post
    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

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QSettings to read an ini file

    Quote Originally Posted by high_flyer View Post
    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    Post your code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QSettings to read an ini file

    Qt Code:
    1. QString grpName = "WINDOW_" + QString::number(i+1);
    2. set.beginGroup(grpName);
    3. CnaWindow *win = new CnaWindow();
    4. win->title= set.value("Title").toString();
    5. win->number= set.value("Number").toInt();
    6. win->position= set.value("Position").toString();
    7. set.endGroup();
    To copy to clipboard, switch view to plain text mode 

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    Hmm...
    What does "contains" return:
    Qt Code:
    1. QString grpName = "WINDOW_" + QString::number(i+1);
    2. set.beginGroup(grpName);
    3. CnaWindow *win = new CnaWindow();
    4. win->title= set.value("Title").toString();
    5. bool bContains = set.contains("Number");
    6. win->number= set.value("Number").toInt();
    7. bContains = set.contains("Position");
    8. win->position= set.value("Position").toString();
    9. set.endGroup();
    To copy to clipboard, switch view to plain text mode 

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

    Also, try this:
    Qt Code:
    1. QStringList keys = set.childKeys();
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QSettings to read an ini file

    '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

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    '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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Jul 2011
    Posts
    7
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: using QSettings to read an ini file

    Quote Originally Posted by high_flyer View Post
    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?

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. The following user says thank you to high_flyer for this useful post:

    gye325 (20th July 2011)

  14. #13
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    I tried with several string values,

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

    this
    Qt Code:
    1. Position="0, 50, 50, 450, 100 ;cmd, x, y, w, h"
    To copy to clipboard, switch view to plain text mode 
    works
    A camel can go 14 days without drink,
    I can't!!!

  15. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  16. #15
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    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
    Last edited by mcosta; 20th July 2011 at 17:41. Reason: updated contents
    A camel can go 14 days without drink,
    I can't!!!

  17. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    Probably he have to use QSettings::registerFormat() in order to create a "special" INI parser
    Yes, this looks like a good way to go.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  18. #17
    Join Date
    Sep 2010
    Posts
    13
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: using QSettings to read an ini file

    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.

Similar Threads

  1. Read Several Groups in INI file from QSettings
    By deepal_de in forum Qt Programming
    Replies: 3
    Last Post: 5th July 2011, 10:43
  2. How to use QSettings to read INI file
    By Cantora in forum Newbie
    Replies: 8
    Last Post: 16th June 2011, 09:14
  3. Replies: 1
    Last Post: 14th January 2011, 12:57
  4. QSettings , read only avaiable?
    By patrik08 in forum Qt Programming
    Replies: 5
    Last Post: 18th November 2007, 16:21
  5. Using QSettings to read pre-made INI file..
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 06:36

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.