PDA

View Full Version : Problem with converting a QSettings value to QString



0signal
23rd April 2011, 13:28
This is my code:


fileName = QFileDialog::getOpenFileName(this, tr("Open Image File"), QDir::currentPath());//QString
settings = new QSettings("Something", "Some Thing");//QSettings
settings->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:


fileName = settings->value("lastimage").toString();

or do I have to do something else ?

xalam
22nd January 2015, 19:20
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:


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, 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();
}

ChrisW67
22nd January 2015, 19:39
Perhaps you are retrieving the value through another QSettings instance that does not have the same organisation and application name.