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.
Code:
Window::Window()
{
restoreRecentFileList();
}
Window::~Window()
{
saveRecentFileList();
}
void Window::saveRecentFileList()
{
settings.
setValue("recentFiles/list",
QVariant(m_recentFilesList
));
}
void Window::restoreRecentFileList()
{
m_recentFilesList = settings.value("recentFiles/list").toStringList();
}
void Window
::openFile(QString l_file
) {
if(isValidFile(l_file))
{
m_recentFilesList << l_file;
qDebug() << "connected to : " << l_file;
}
else
{
}
}
//when i print m_recentFilesList , i am able to see expected values
Code:
on 1st run of Application: no .ini file , after closing App : .ini file have 1 file name stored in it
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
on 1st run of Application: .ini file is empty , ini file is empty
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.
Re: QSettings rewriting the .ini file, so not able to restore the values
Sorry, I forgot to update that, here is the constructor actually
Quote:
Window::Window(QWidget *parent) : QDialog(parent),
m_settingsPath(QDir::homePath() + QString::fromUtf8("/.cfg/recentFiles.ini"))
{
//m_settingsPath is a member variable of the class
}
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.
Re: QSettings rewriting the .ini file, so not able to restore the values
Quote:
Originally Posted by
ChrisW67
What actually gets into the ini file?
I am just storing file paths, below is the sample file content
Quote:
[recentFiles]
list=/home/durgaprasad/temp/test.xml
Quote:
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.
Quote:
$ 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
Re: QSettings rewriting the .ini file, so not able to restore the values
Quote:
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.
Re: QSettings rewriting the .ini file, so not able to restore the values
Quote:
Originally Posted by
ChrisW67
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,
_
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.
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.