PDA

View Full Version : Application reverts to old stored variable instead of the updated variable



QT-wannabe
6th February 2019, 00:47
Hello,

I am working on a project where a user selects a theme option. When the app reloads it should display the new theme. Currently, the program launches by searching the QSettings through a OptionsModel.cpp class looking for variables stored. If not stored, it returns the default variable. The UI's are then loaded and pull from this code to grab the correct theme.


QString loadStyleSheet()
{
QString styleSheet;
QSettings settings;
QString cssName;
QString theme = settings.value("theme", "").toString();
if(!theme.isEmpty()){
cssName = QString(":/css/") + theme;
}

else {
cssName = QString(":/css/light");
settings.setValue("theme", "light");
}

QFile qFile(cssName);
if (qFile.open(QFile::ReadOnly)) {
styleSheet = QLatin1String(qFile.readAll());
}

return styleSheet;
}


The correct UI css should be loaded. If someone wants to change the theme they navigate to the options dialog and select the dark option from the dropdown menu. This is mapped as a change and stored into the theme variable with settings.setValue("theme", theme). using GUIUtil::getThemeName which is very similar to the above code but for the theme variable instead. Through debugging I have been able to successfully print the theme variable as dark. I ran settings.sync() to store the variable and update the setting storage. When I restart the application to launch the dark theme, the light theme variable remains in the Qsettings. Additionally, if I set the default variable of the code above to dark, ie if there was nothing being stored in settings it should go with the dark theme, it doesn't go with the dark theme and again reverts to the light theme.


I'm building on Ubuntu 16.04 and I have no build errors. I know I'm not giving y'all a lot of information on the problem, but I hoped to give enough that if someone had any ideas as to why the wrong variable is being stored they would be able to help.

Thank You in advance

QT-wannabe
6th February 2019, 04:18
Hello,

I am working on a project where a user selects a theme option. When the app reloads it should display the new theme. Currently, the program launches by searching the QSettings through a OptionsModel.cpp class looking for variables stored. If not stored, it returns the default variable. The UI's are then loaded and pull from this code to grab the correct theme.


QString loadStyleSheet()
{
QString styleSheet;
QSettings settings;
QString cssName;
QString theme = settings.value("theme", "").toString();
if(!theme.isEmpty()){
cssName = QString(":/css/") + theme;
}

else {
cssName = QString(":/css/light");
settings.setValue("theme", "light");
}

QFile qFile(cssName);
if (qFile.open(QFile::ReadOnly)) {
styleSheet = QLatin1String(qFile.readAll());
}

return styleSheet;
}


The correct UI css should be loaded. If someone wants to change the theme they navigate to the options dialog and select the dark option from the dropdown menu. This is mapped as a change and stored into the theme variable with settings.setValue("theme", theme). using GUIUtil::getThemeName which is very similar to the above code but for the theme variable instead. Through debugging I have been able to successfully print the theme variable as dark. I ran settings.sync() to store the variable and update the setting storage. When I restart the application to launch the dark theme, the light theme variable remains in the Qsettings. Additionally, if I set the default variable of the code above to dark, ie if there was nothing being stored in settings it should go with the dark theme, it doesn't go with the dark theme and again reverts to the light theme.


I'm building on Ubuntu 16.04 and I have no build errors. I know I'm not giving y'all a lot of information on the problem, but I hoped to give enough that if someone had any ideas as to why the wrong variable is being stored they would be able to help.

Thank You in advance

anda_skoa
6th February 2019, 08:13
Have you checked the INI file after changing the theme?
Does it contain the correct value?

Cheers,
_

QT-wannabe
6th February 2019, 20:37
Have you checked the INI file after changing the theme?
Does it contain the correct value?

Cheers,
_

The file remains light although my debugs are saying that settings.value("theme", "") is dark. Besides settings.setValue is there another way to write to the INI file? I have noticed that before I started trying to change the theme that I could change the language and have it saved to the INI. I must have broken the save mechanism somehow?

Added after 1 30 minutes:

Turns out I did indeed delete something I wasn't suppose to. Starting over everything builds and saves perfectly.