Results 1 to 9 of 9

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 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,
    _

  6. #6
    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.

  7. #7
    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, 06:59
  2. using QSettings to read an ini file
    By gye325 in forum Qt Programming
    Replies: 16
    Last Post: 2nd September 2011, 23:07
  3. Replies: 3
    Last Post: 26th July 2011, 17:59
  4. How to use QSettings to read INI file
    By Cantora in forum Newbie
    Replies: 8
    Last Post: 16th June 2011, 08:14
  5. Using QSettings to read pre-made INI file..
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 05: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.