Can't get QSettings to write my settings properly

193 Views Asked by At

So I have the tutorial project Notepad. I am trying to use QSettings so that it saves the font,font size and other font related options in a file but it never loads them properly and when I check the config file, the values look quite weird.

void Notepad::saveSettings()
{
    QSettings setting("MyTE","myte");
    QFont font = this->font();
    setting.beginGroup("MainWindow");
    setting.setValue("text.font",font.toString());
    setting.setValue("text.font.family",font.family());
    setting.setValue("text.font.size",font.pointSize());
    setting.setValue("text.font.bold",font.bold());
    setting.setValue("text.font.italic",font.italic());
    setting.endGroup();
    qDebug() << "Saved";
}

void Notepad::loadSettings(){
    QSettings setting ("MyTE","myte");
    QFont font = setting.value("text.font",QString()).toString();
    setting.beginGroup("MainWindow");
    QString fontFamily = setting.value("text.font.family",QString()).toString();
    int fontSize = setting.value("text.font.size",12).toInt();
    setting.setValue("text.font.size",fontSize);
    //bool fontIsBold = setting.value("text.font.bold",false).toBool();
    //bool fontIsItalic = setting.value("text.font.italic",false).toBool();
    setFont(font);
    font.setPointSize(fontSize);
    setting.endGroup();
    qDebug() << "Loaded";

}

I uncommented the bold ones because they simply don't work. I have 2 buttons that correspond to these 2 functions and I also call loadSettings() when the app first starts.

Here is the output in the config file, that never changes and just resets to these weird values.

[MainWindow]
text.font=",9,-1,5,50,0,0,0,0,0"
text.font.bold=false
text.font.family=
text.font.italic=false
text.font.size=9

0

There are 0 best solutions below