Problem with converting a QSettings value to QString
This is my code:
Code:
fileName
= QFileDialog::getOpenFileName(this, tr
("Open Image File"),
QDir::currentPath());
//QStringsettings
= new QSettings("Something",
"Some Thing");
//QSettingssettings->setValue("lastimage", fileName);
The value appears in the Windows registry as it should be, but when i try to convert it back to QString it returns an empty string (the value field in the registry key is empty).
Is there something wrong with this code:
Code:
fileName = settings->value("lastimage").toString();
or do I have to do something else ?
Re: Problem with converting a QSettings value to QString
I had the same problem but figured it out. I was setting the value on a button click and retrieving in constructor of main window. I am not using QSettings constructor parameters but instead using QCoreApplication's setOrganizationName() and setApplicationName().
The problem window was being created before I was setting applications parameters, fixing the order fixed the issue. So overall the solution was:
Code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
// these functions should be here
a.setOrganizationName("Organization");
a.setApplicationName("MyApp");
MainWindow w;
w.show();
// not here (where I had them previously.
return a.exec();
}
Re: Problem with converting a QSettings value to QString
Perhaps you are retrieving the value through another QSettings instance that does not have the same organisation and application name.