PDA

View Full Version : QSettings rewriting the .ini file, so not able to restore the values



DURGAPRASAD NEELAM
19th June 2015, 10:29
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.


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

Window::~Window()
{
saveRecentFileList();
}

void Window::saveRecentFileList()
{
QSettings settings(m_settingsPath, QSettings::NativeFormat);
settings.setValue("recentFiles/list", QVariant(m_recentFilesList));
}

void Window::restoreRecentFileList()
{
QSettings settings(m_settingsPath, QSettings::NativeFormat);
m_recentFilesList = settings.value("recentFiles/list").toStringList();
}

void Window::openFile(QString l_file)
{

if(isValidFile(l_file))
{
m_recentFilesList << l_file;

done(QDialog::Accepted);
qDebug() << "connected to : " << l_file;
}
else
{
QMessageBox::information(this, "Information", "Please choose valid file", QMessageBox::Ok);
}
}



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



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

ChrisW67
19th June 2015, 12:07
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.

DURGAPRASAD NEELAM
19th June 2015, 12:16
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
}

ChrisW67
19th June 2015, 12:39
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.

DURGAPRASAD NEELAM
19th June 2015, 13:35
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

d_stranz
19th June 2015, 16:58
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.

anda_skoa
20th June 2015, 13:22
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,
_

DURGAPRASAD NEELAM
22nd June 2015, 12:41
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.

ChrisW67
22nd June 2015, 22:24
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.