Results 1 to 9 of 9

Thread: QSettings rewriting the .ini file, so not able to restore the values

  1. #1
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSettings rewriting the .ini file, so not able to restore the values

    When I am using below code on Linux SLES 11 machine the .ini file is recreating for some reason so that I am not able to restore my settings.
    Same code working fine on Windows machine.

    Prb: I want to have recently opened file list on start up window, so that user can select quickly.

    Qt Code:
    1. Window::Window()
    2. {
    3. restoreRecentFileList();
    4. }
    5.  
    6. Window::~Window()
    7. {
    8. saveRecentFileList();
    9. }
    10.  
    11. void Window::saveRecentFileList()
    12. {
    13. QSettings settings(m_settingsPath, QSettings::NativeFormat);
    14. settings.setValue("recentFiles/list", QVariant(m_recentFilesList));
    15. }
    16.  
    17. void Window::restoreRecentFileList()
    18. {
    19. QSettings settings(m_settingsPath, QSettings::NativeFormat);
    20. m_recentFilesList = settings.value("recentFiles/list").toStringList();
    21. }
    22.  
    23. void Window::openFile(QString l_file)
    24. {
    25.  
    26. if(isValidFile(l_file))
    27. {
    28. m_recentFilesList << l_file;
    29.  
    30. done(QDialog::Accepted);
    31. qDebug() << "connected to : " << l_file;
    32. }
    33. else
    34. {
    35. QMessageBox::information(this, "Information", "Please choose valid file", QMessageBox::Ok);
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 

    //when i print m_recentFilesList , i am able to see expected values


    Qt Code:
    1. on 1st run of Application: no .ini file , after closing App : .ini file have 1 file name stored in it
    2. on 2st run of Application: .ini file have 1 file name stored in it , after closing App : .ini file have 1 file name stored in it
    3. on 1st run of Application: .ini file is empty , ini file is empty
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings rewriting the .ini file, so not able to restore the values

    Based on the code you posted m_settingsPath, which we have to assume is a member variable of the class, has not been initialised at the time restoreRecentFileList() is called.

  3. #3
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings rewriting the .ini file, so not able to restore the values

    Sorry, I forgot to update that, here is the constructor actually

    Window::Window(QWidget *parent) : QDialog(parent),
    m_settingsPath(QDir::homePath() + QString::fromUtf8("/.cfg/recentFiles.ini"))
    {
    //m_settingsPath is a member variable of the class
    }

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings rewriting the .ini file, so not able to restore the values

    What actually gets into the ini file?

    BTW: QDir::homePath() may not be where you think it is if there is no HOME environment in the running program's environment.

  5. #5
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings rewriting the .ini file, so not able to restore the values

    Quote Originally Posted by ChrisW67 View Post
    What actually gets into the ini file?

    I am just storing file paths, below is the sample file content

    [recentFiles]
    list=/home/durgaprasad/temp/test.xml

    BTW: QDir::homePath() may not be where you think it is if there is no HOME environment in the running program's environment.
    No the file is creating in the home dir & there is home environment setup already. but some how the file becoming empty on SLES11 machine only.

    I observed file recreation when i run my application at different times.


    $ ll | grep rece*
    -rw-r----- 1 durgaprasad users 57 Jun 19 16:43 recentFiles.ini

    $ ll | grep rece*
    -rw-r----- 1 durgaprasad users 0 Jun 19 16:56 recentFiles.ini
    Last edited by DURGAPRASAD NEELAM; 19th June 2015 at 13:32.

  6. #6
    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: QSettings rewriting the .ini file, so not able to restore the values

    Window::Window()
    {
    restoreRecentFileList();
    }

    Window::~Window()
    {
    saveRecentFileList();
    }
    I usually do this in the showEvent() and closeEvent(), respectively, rather than in the constructor and destructor. In the events, the instance is in a known state, not in the middle of being built or taken apart. It probably makes no difference, but since it appears that you are using Window as a modal dialog (eg. the call to done()), this might have some bearing on it. You might be getting caught in some timing issue involved in flushing the settings to a file and the destruction of the Window object.

    As an alternative, you could move the handling of the recent file list out of the Window class and into the application: make a slot / signal pair - a slot to tell the Window that the recent file list has changed, the signal that the Window uses to announce that it has changed the list. The QSettings handling then moves to the QWindow that handles the lifetime of the Window class, which presumably sticks around longer.

  7. #7
    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: QSettings rewriting the .ini file, so not able to restore the values

    Quote Originally Posted by ChrisW67 View Post
    BTW: QDir::homePath() may not be where you think it is if there is no HOME environment in the running program's environment.
    While I don't think it is likely that QDir::homePath() does not return something usable, the proper location for config files is of course the writable location of QStandardPaths::ConfigLocation.

    Cheers,
    _

  8. #8
    Join Date
    Jan 2013
    Location
    Bangalore
    Posts
    49
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings rewriting the .ini file, so not able to restore the values

    There is something wrong with my Environment I think, It's working as expected on another SLES machine, sorry for that and thanks for the effort.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSettings rewriting the .ini file, so not able to restore the values

    Another possibility is that your code constructs two Window objects (deliberately or not), one is actively used, the other is dormant so keeps the originally loaded list, and the last one to be destroyed wins. Stick a breakpoint/qDebug() in the destructor to see how many objects of this class are destroyed as you leave the program.

Similar Threads

  1. Replies: 9
    Last Post: 15th October 2013, 07:59
  2. using QSettings to read an ini file
    By gye325 in forum Qt Programming
    Replies: 16
    Last Post: 3rd September 2011, 00:07
  3. Replies: 3
    Last Post: 26th July 2011, 18:59
  4. How to use QSettings to read INI file
    By Cantora in forum Newbie
    Replies: 8
    Last Post: 16th June 2011, 09:14
  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.