Results 1 to 14 of 14

Thread: QSettings

  1. #1
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings

    I am using :
    Qt Code:
    1. settings.value( "coco", "ERROR" ).toString();
    To copy to clipboard, switch view to plain text mode 
    this works

    Qt Code:
    1. const QString s = "coco";
    2. settings.value( s, "ERROR" ).toString();
    To copy to clipboard, switch view to plain text mode 
    this insists on returning "ERROR"

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

    s being a const string

  2. #2
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSettings

    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?
    Software Engineer



  3. The following user says thank you to gfunk for this useful post:

    incapacitant (24th May 2006)

  4. #3
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings

    Qt Code:
    1. QString x = "TxtCharSMSMax";
    2. qS = settings.value( x, "ERROR" ).toString();
    To copy to clipboard, switch view to plain text mode 

    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.

  5. #4
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSettings

    Weird. What's the exact text message of the error message from the compiler? Are you using Visual C++ or MinGW?
    Software Engineer



  6. The following user says thank you to gfunk for this useful post:

    incapacitant (24th May 2006)

  7. #5
    Join Date
    Jan 2006
    Location
    South Carolina, USA
    Posts
    34
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings

    Quote Originally Posted by incapacitant
    I am using :
    Qt Code:
    1. settings.value( "coco", "ERROR" ).toString();
    To copy to clipboard, switch view to plain text mode 
    this works

    Qt Code:
    1. const QString s = "coco";
    2. settings.value( s, "ERROR" ).toString();
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. #include <QString>
    2. #include <QSettings>
    3. #include <iostream>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QSettings settings("TestSoft", "QSettings Test");
    8. settings.setValue("testName", 100);
    9. const QString s = "testName";
    10. std::cout << "Test call with char string: "
    11. << settings.value("testName", "ERROR").toString().toStdString() << std::endl
    12. << "Test call with QString: "
    13. << settings.value(s, "ERROR").toString().toStdString() << std::endl
    14. << "For good measure create error: "
    15. << settings.value("doesNotExist", "ERROR").toString().toStdString() << std::endl;
    16. char t;
    17. std::cin >> t;
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by michael; 24th May 2006 at 21:49.

  8. The following user says thank you to michael for this useful post:

    incapacitant (24th May 2006)

  9. #6
    Join Date
    Apr 2006
    Location
    San Francisco, CA
    Posts
    186
    Thanks
    55
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSettings

    So what is the text of the problem when you try to use "QString s" instead of "const QString s"?
    Software Engineer



  10. The following user says thank you to gfunk for this useful post:

    incapacitant (24th May 2006)

  11. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings

    I think there is no compiler error, just the key is not found in the ini file.

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

  12. The following user says thank you to wysota for this useful post:

    incapacitant (24th May 2006)

  13. #8
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings

    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:
    Qt Code:
    1. ApplicationWindow::ApplicationWindow()
    2. : QMainWindow( 0 )
    3. {
    4. QCoreApplication::setOrganizationName("Adrien");
    5. QCoreApplication::setApplicationName("EFREI SMS");
    6. QSettings settings("efrei.ini", QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 

    I had to put it public in this class AND pass it as parameter :
    Qt Code:
    1. qS = iniGet( settings, "TxtCharSMSMax" ) + " " + qS2;
    To copy to clipboard, switch view to plain text mode 

    to this procedure :
    Qt Code:
    1. QString ApplicationWindow::iniGet( QSettings &settings, const QString &s )
    2. {
    3. QString parameter;
    4. QString qS3;
    5. QString qS4;
    6. parameter = settings.value( s, "ERROR" ).toString();
    7. if ( parameter == "ERROR" )
    8. {
    9. qS3 = "Unable to load parameter from efrei.ini";
    10. qS4 = s + " not found. Replaced with 'Error'";
    11. QMessageBox::critical( this, qS3, qS4 );
    12. parameter = "Error";
    13. }
    14. return parameter;
    15. }
    To copy to clipboard, switch view to plain text mode 

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

  14. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings

    You probably didn't initialize it properly.

  15. #10
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings

    What do you mean initialize. I wrote :
    Qt Code:
    1. class ApplicationWindow: public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. private:
    6. QSettings settings;
    To copy to clipboard, switch view to plain text mode 

    What else is needed.

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

  16. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings

    Compare those two:

    Qt Code:
    1. QSettings settings("efrei.ini", QSettings::IniFormat);
    2. QSettings settings;
    To copy to clipboard, switch view to plain text mode 

  17. #12
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings

    QSettings is initialised here :
    Qt Code:
    1. ApplicationWindow::ApplicationWindow()
    2. : QMainWindow( 0 )
    3. {
    4. QCoreApplication::setOrganizationName("Adrien");
    5. QCoreApplication::setApplicationName("EFREI SMS");
    6. QSettings settings("efrei.ini", QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 


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

  18. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSettings

    Quote Originally Posted by incapacitant
    QSettings is initialised here :
    Qt Code:
    1. ApplicationWindow::ApplicationWindow()
    2. : QMainWindow( 0 )
    3. {
    4. QCoreApplication::setOrganizationName("Adrien");
    5. QCoreApplication::setApplicationName("EFREI SMS");
    6. QSettings settings("efrei.ini", QSettings::IniFormat);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QCoreApplication::setOrganizationName("Adrien"); // you should put that in main.cpp
    2. QCoreApplication::setApplicationName("EFREI SMS"); // and this too
    3. //...
    4. ApplicationWindow::ApplicationWindow()
    5. : QMainWindow( 0 ), settings("efrei.ini", QSettings::IniFormat)
    6. {
    To copy to clipboard, switch view to plain text mode 

  19. The following user says thank you to wysota for this useful post:

    incapacitant (25th May 2006)

  20. #14
    Join Date
    Jan 2006
    Location
    Grenoble, France
    Posts
    165
    Thanks
    106
    Qt products
    Qt4
    Platforms
    Windows

    Default QSettings

    It works and I meet you I will ask for a brain transplant.

Similar Threads

  1. QSettings vs (QFile + Qtextstream)
    By nupul in forum Newbie
    Replies: 5
    Last Post: 10th April 2006, 08:26
  2. Replies: 4
    Last Post: 1st February 2006, 18:17
  3. QSettings again ... how to remove array elements
    By Mike in forum Qt Programming
    Replies: 4
    Last Post: 11th January 2006, 09:58
  4. QSettings - beginReadArray not working
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 9th January 2006, 22:24

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.