Results 1 to 12 of 12

Thread: How to read .ini file from resources files with QSettings?

  1. #1
    Join Date
    Dec 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question How to read .ini file from resources files with QSettings?

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QSettings>
    4. #include <QDebug>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. QSettings * qsettings = new QSettings(":/config.ini",QSettings::IniFormat);
    12. bool status = qsettings->value("preview","").toBool();
    13. qDebug() << status;
    14.  
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Once i could do it but now i don't know whats wrong. When i googled this problem i just saw that this impossible but i enshure that i did it before.

  2. #2
    Join Date
    Feb 2014
    Posts
    60
    Thanks
    4
    Thanked 5 Times in 5 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read .ini file from resources files with QSettings?

    HI,
    Try this it will help you to retrieve all values from file and then you can iterate to get the particular value.

    Qt Code:
    1. QSettings settings("config.ini", QSettings::IniFormat);
    2. settings.beginGroup("General");
    3. QStringList keys = settings.allKeys();
    To copy to clipboard, switch view to plain text mode 

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

    orkto (21st December 2014)

  4. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to read .ini file from resources files with QSettings?

    The second parameter of value() is a default value if the key (the first parameter) has not been found. Therefore:
    Qt Code:
    1. qsettings->beginGroup("General");
    2. bool status = qsettings->value("preview",false).toBool();
    3. qsettings->endGroup();
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. qsettings->beginGroup("General");
    2. bool status = qsettings->value("preview",true).toBool();
    3. qsettings->endGroup();
    To copy to clipboard, switch view to plain text mode 
    If you want to check whether you are reading config.ini correctly, use the first variant.
    Last edited by Radek; 21st December 2014 at 11:12.

  5. The following user says thank you to Radek for this useful post:

    orkto (21st December 2014)

  6. #4
    Join Date
    Dec 2014
    Posts
    3
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read .ini file from resources files with QSettings?

    Thx guys. The problem is Qt creator don't include resource file until launch qmake. Strange behavior.

  7. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read .ini file from resources files with QSettings?

    Why would you want to store config information in the app's resources? This means they can't be changed at run time, because that would require rebuilding the executable. And since you have to run the resource compiler and relink the executable every time the resources change, why bother using ini format at all?

  8. #6
    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: How to read .ini file from resources files with QSettings?

    Quote Originally Posted by orkto View Post
    Thx guys. The problem is Qt creator don't include resource file until launch qmake. Strange behavior.
    You probably mean that if you modify the ini file already added to the resource file that doesn't cause the resource file to be updated with the new content. I'd say this is normal and expected, I wouldn't expect make to read and parse my resource files upon every invocation. That would dramatically increase compilation time.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to read .ini file from resources files with QSettings?

    I'd say this is normal and expected, I wouldn't expect make to read and parse my resource files upon every invocation.
    Visual Studio appears to take a different approach. If I edit a file included as a resource (like an icon, for example), the resource file (qrc) is recompiled next time around.

  10. #8
    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: How to read .ini file from resources files with QSettings?

    Quote Originally Posted by d_stranz View Post
    Visual Studio appears to take a different approach. If I edit a file included as a resource (like an icon, for example), the resource file (qrc) is recompiled next time around.
    Visual Studio doesn't use qmake so it has no other choice.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to read .ini file from resources files with QSettings?

    Quote Originally Posted by wysota View Post
    You probably mean that if you modify the ini file already added to the resource file that doesn't cause the resource file to be updated with the new content. I'd say this is normal and expected, I wouldn't expect make to read and parse my resource files upon every invocation. That would dramatically increase compilation time.
    Hmm.
    I haven't used this lately, but I vaguely remember that in a project which had QML files in a resource any change made to a QML file did make it into the next build.

    Cheers,
    _

  12. #10
    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: How to read .ini file from resources files with QSettings?

    Quote Originally Posted by anda_skoa View Post
    Hmm.
    I haven't used this lately, but I vaguely remember that in a project which had QML files in a resource any change made to a QML file did make it into the next build.
    I remember it worked in some conditions but not in others. Haven't tried that lately, especially without Qt Creator. Make would have to have a dependency set on the contents of the qrc file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to read .ini file from resources files with QSettings?

    Quote Originally Posted by wysota View Post
    I remember it worked in some conditions but not in others. Haven't tried that lately, especially without Qt Creator. Make would have to have a dependency set on the contents of the qrc file.
    Right.
    I think it works if the files are specified explicitly and does not if specified by wildcards.

    Cheers,
    _

  14. #12
    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: How to read .ini file from resources files with QSettings?

    It could be that Creator touches the qrc file whenever it saves a qml file belonging to the resource. This would cause make to rebuild it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. using QSettings to read an ini file
    By gye325 in forum Qt Programming
    Replies: 16
    Last Post: 3rd September 2011, 00:07
  2. Read Several Groups in INI file from QSettings
    By deepal_de in forum Qt Programming
    Replies: 3
    Last Post: 5th July 2011, 10:43
  3. How to use QSettings to read INI file
    By Cantora in forum Newbie
    Replies: 8
    Last Post: 16th June 2011, 09:14
  4. Replies: 1
    Last Post: 19th March 2011, 03:42
  5. Using QSettings to read pre-made INI file..
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 06:36

Tags for this Thread

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.