[Solved] Qsettings dont save the values



  • Hello I have a problem with Qsettings. It dont save values. Just One "ZaciatokCykluDen". Another values are ignored and when I start my app, They are null.

    there si the code:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSettings>
    #include "time.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    QCoreApplication::setOrganizationName("MADNESS s.r.o");
    QCoreApplication::setOrganizationDomain("madnes.eu");
    QCoreApplication::setApplicationName("Ovulačný kalendár");

    ui->setupUi(this);
    
    //settings.setProperty();
    
    // nacitame nastavenia
    bool ok = this->nacitatNastavenia();
    
    // ak nastavenia neexistuju aplikacia je spustena prvý krát
    if(ok)
    {
        ui->but1->hide();
        this->nastavitNastavenia();
    }
    else
    {
        // nastavia sa predvolené hodnoty do nastavení
        this->nastavitNastaveniaNaPredvolene();
    
        // prepne sa na option2 nastavenia
        this->changeToOption2();
    }
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::changeToOption1()
    {

    }

    void MainWindow::changeToOption2()
    {

    }

    void MainWindow::changeToOption3()
    {

    }

    void MainWindow::nastavitNastavenia()
    {
    QSettings settings("ovulacnykalendar");

    settings.setValue("ovulacny/cyklus",this->cyklus);
    
    //int value = settings.value("cyklus").toInt();
    //ui->luteal->setValue(value);
    
    settings.setValue("ovulacny/luteal",this->luteal);
    settings.setValue("ovulacny/ciel",this->ciel);
    settings.setValue("ovulacny/ZaciatokCykluDen",this->ZaciatokCykluDen);
    settings.setValue("ovulacny/ZaciatokCykluMesiac",this->ZaciatokCykluMesiac);
    settings.setValue("ovulacny/ZaciatokCykluRok",this->ZaciatokCykluRok);
    settings.sync();
    
    // nastavime hodnoty vo formulari nastavenia
    ui->ZaciatokCykluDen->setValue(this->ZaciatokCykluDen);
    ui->ZaciatokCykluMesiac->setValue(this->ZaciatokCykluMesiac);
    ui->ZaciatokCykluRok->setValue(this->ZaciatokCykluRok);
    
    ui->cyklus->setValue(this->cyklus);
    ui->luteal->setValue(this->luteal);
    
    if(this->ciel == 0)
    {
        ui->ciel1->setChecked(true);
        ui->ciel2->setChecked(false);
    }
    else
    {
        ui->ciel1->setChecked(false);
        ui->ciel2->setChecked(true);
    }
    

    }

    void MainWindow::nastavitNastaveniaNaPredvolene()
    {
    return;
    // nastavime this hodnoty
    this->cyklus = 28;
    this->luteal = 14;
    this->ciel = 1;

    time_t cas; tm *str;         //vytvorenie pointru typu tm
    time(&cas);
    
    str = localtime(&cas);
    
    this->ZaciatokCykluDen = str->tm_mday;
    this->ZaciatokCykluMesiac = str->tm_mon;
    this->ZaciatokCykluRok = str->tm_year;
    
    // ulozime hodnoty
    this->nastavitNastavenia();
    

    }

    bool MainWindow::nacitatNastavenia()
    {
    QSettings settings("ovulacnykalendar");

    this->cyklus = settings.value("ovulacny/cyklus",24).toInt();
    this->luteal = settings.value("ovulacny/luteal").toInt();
    this->ciel = settings.value("ovulacny/ciel").toInt();
    
    this->ZaciatokCykluDen = settings.value("ovulacny/ZaciatokCykluDen").toInt();
    this->ZaciatokCykluMesiac = settings.value("ovulacny/ZaciatokCykluMesiac",11).toInt();
    this->ZaciatokCykluRok = settings.value("ovulacny/ZaciatokCykluRok",2010).toInt();
    
    bool returning = settings.contains("ovulacny/cyklus");
    
    settings.sync();
    return returning;
    

    }

    void MainWindow::ulozitNastavenia(int value)
    {
    this->cyklus = ui->cyklus->value();
    this->luteal = ui->luteal->value();

    if(ui->ciel1->isChecked())
        this->ciel = 0;
    
    if(ui->ciel2->isChecked())
        this->ciel = 1;
    
    this->ZaciatokCykluDen = ui->ZaciatokCykluDen->value();
    this->ZaciatokCykluMesiac = ui->ZaciatokCykluMesiac->value();
    this->ZaciatokCykluRok = ui->ZaciatokCykluRok->value();
    
    this->nastavitNastavenia();
    

    }
    @

    Thank you for help.



  • I don't see this function: ulozitNastavenia(int) called somewhere...

    Try something like this:

    @
    this->ZaciatokCykluDen = ui->ZaciatokCykluDen->value();
    this->ZaciatokCykluMesiac = ui->ZaciatokCykluMesiac->value();
    this->ZaciatokCykluRok = ui->ZaciatokCykluRok->value();

    settings.setValue("ovulacny/ZaciatokCykluDen",this->ZaciatokCykluDen);
    settings.setValue("ovulacny/ZaciatokCykluMesiac",this->ZaciatokCykluMesiac);
    settings.setValue("ovulacny/ZaciatokCykluRok",this->ZaciatokCykluRok);
    

    @

    Or

    @
    settings.setValue("ovulacny/ZaciatokCykluDen",ui->ZaciatokCykluDen->value());
    settings.setValue("ovulacny/ZaciatokCykluMesiac",ui->ZaciatokCykluMesiac->value());
    settings.setValue("ovulacny/ZaciatokCykluRok",ui->ZaciatokCykluRok->value());
    @

    On your save settings function...



  • it is slot, called when spinbox change value. This function is called I test this.



  • okay, have you tried changing the code as I advised previously?



  • Yes I change it, but it not runs. Just one variable is saved : "ZaciatokCykluDen".

    @void MainWindow::ulozitNastavenia(int value)
    {
    QSettings settings("ovulacnykalendar");

    this->cyklus = ui->cyklus->value();
    this->luteal = ui->luteal->value();
    
    if(ui->ciel1->isChecked())
    {
        this->ciel = 0;
        settings.setValue("ovulacny/ciel",0);
    }
    
    if(ui->ciel2->isChecked())
    {
        this->ciel = 1;
         settings.setValue("ovulacny/ciel",1);
    }
    
    this->ZaciatokCykluDen = ui->ZaciatokCykluDen->value();
    this->ZaciatokCykluMesiac = ui->ZaciatokCykluMesiac->value();
    this->ZaciatokCykluRok = ui->ZaciatokCykluRok->value();
    
    settings.setValue("ovulacny/cyklus",ui->cyklus->value());
    
    settings.setValue("ovulacny/luteal",ui->luteal->value());
    
    settings.setValue("ovulacny/ZaciatokCykluDen",ui->ZaciatokCykluDen->value());
    settings.setValue("ovulacny/ZaciatokCykluMesiac",ui->ZaciatokCykluMesiac->value());
    settings.setValue("ovulacny/ZaciatokCykluRok",ui->ZaciatokCykluRok->value());
    settings.sync();
    

    }
    @



  • is there another way to save variables ?



  • Do the two other variables possess values? Can you print the values on the console before saving them and inspect if they take the values... you can either debug or use qDebug as below:

    @
    qDebug() << ui->ZaciatokCykluDen->value();
    qDebug() << ui->ZaciatokCykluMesiac->value();
    qDebug() << ui->ZaciatokCykluRok->value();
    @

    if you are expecting QString from value() function, don't forget to add qPrintable():

    @
    qDebug() << qPrintable(ui->ZaciatokCykluDen->value());
    qDebug() << qPrintable(ui->ZaciatokCykluMesiac->value());
    qDebug() << qPrintable(ui->ZaciatokCykluRok->value());
    @



  • this not run - return many errors. But i tried insert value in another spinbox and other variables has values before saving into settings.



  • [quote author="tristenn" date="1292342936"]this not run - return many errors. But i tried insert value in another spinbox and other variables has values before saving into settings.[/quote]

    It would be helpful if you told us which errors actually occured.



  • This is run:
    @settings.setValue("ovulacny/ZaciatokCykluMesiac",ui->ZaciatokCykluMesiac->value());
    ui->cyklus->setValue(settings.value("ovulacny/ZaciatokCykluMesiac").toInt());@

    but if i close app and then start again. Values are null.



  • [quote author="Milot Shala" date="1292342470"]if you are expecting QString from value() function, don't forget to add qPrintable():

    @
    qDebug() << qPrintable(ui->ZaciatokCykluDen->value());
    @[/quote]

    This is not necessary for qDebug() with operator<<, it takes a plain QString as an argument (and many other types, see "QDebug's public functions":http://doc.qt.nokia.com/latest/qdebug.html#public-functions; some Qt classes add even more. qPrintable() is needed in the old (qt3-ish) printf-like form of qDebug("format string", arg1, arg2).

    [edit: quote syntax corrected, volker]



  • bq. It would be helpful if you told us which errors actually occured.

    Errors about qDebug like invalid use of incomlete type struct Qdebug



  • Have you include this:
    @#include <QtDebug>@

    [quote author="tristenn" date="1292343392"]bq. It would be helpful if you told us which errors actually occured.

    Errors about qDebug like invalid use of incomlete type struct Qdebug[/quote]



  • [quote author="Volker" date="1292343312"][quote author="Milot Shala" date="1292342470"]if you are expecting QString from value() function, don't forget to add qPrintable():

    @
    qDebug() << qPrintable(ui->ZaciatokCykluDen->value());
    @[/quote]

    This is not necessary for qDebug() with operator<<, it takes a plain QString as an argument (and many other types, see "QDebug's public functions":http://doc.qt.nokia.com/latest/qdebug.html#public-functions; some Qt classes add even more. qPrintable() is needed in the old (qt3-ish) printf-like form of qDebug("format string", arg1, arg2).

    [edit: quote syntax corrected, volker][/quote]

    You are right, but why I suggested to use qPrintable() is that Qt recommends to use qPrintable() if you have QStrings because of the codecs and encoded text.



  • [quote author="tristenn" date="1292343181"]This is run:
    @settings.setValue("ovulacny/ZaciatokCykluMesiac",ui->ZaciatokCykluMesiac->value());
    ui->cyklus->setValue(settings.value("ovulacny/ZaciatokCykluMesiac").toInt());@

    but if i close app and then start again. Values are null.
    [/quote]

    Have you tried calling the function which sets the values in the destructor? That means when your application will exit, your destructor will be called after exiting from the event loop (assuming you instantiated your main class in stack on main function).

    Try this:

    @
    MainWindow::~MainWindow()
    {
    nastavitNastavenia();
    delete ui;
    }

    @



  • [quote author="Milot Shala" date="1292367329"]You are right, but why I suggested to use qPrintable() is that Qt recommends to use qPrintable() if you have QStrings because of the codecs and encoded text.[/quote]

    That's only necessary with the printf based old qPrintable(). QDebug with operator<< works on a QTextStream which handles the encoding issues much better.

    But that's getting quite off topic - we could discuss that further in a new thread, if you want.



  • Ok solution is:

    ulozNastavenia is called on construct, when I call function nastavit nastavenia, because ulozNastavenia is slot called in formchange signal. This is problem, because valuse are saving before I set form values from QSettings.

    solution:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QSettings>
    #include "time.h"
    #include <QtDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    QCoreApplication::setOrganizationName("MADNESS s.r.o");
    QCoreApplication::setOrganizationDomain("madnes.eu");
    QCoreApplication::setApplicationName("Ovulačný kalendár");

    ui->setupUi(this);
    
    this->SettingsLoaded = false;
    
    //settings.setProperty();
    
    // nacitame nastavenia
    bool ok = this->nacitatNastavenia();
    
    // ak nastavenia neexistuju aplikacia je spustena prvý krát
    if(ok)
    {
        ui->but1->hide();
        this->nastavitNastavenia();
    }
    else
    {
        // nastavia sa predvolené hodnoty do nastavení
        this->nastavitNastaveniaNaPredvolene();
    
        // prepne sa na option2 nastavenia
        this->changeToOption2();
    }
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::changeToOption1()
    {

    }

    void MainWindow::changeToOption2()
    {

    }

    void MainWindow::changeToOption3()
    {

    }

    void MainWindow::nastavitNastavenia()
    {
    QSettings settings("ovulacnykalendar");

    settings.setValue("ovulacny/cyklus",this->cyklus);
    
    //int value = settings.value("cyklus").toInt();
    //ui->luteal->setValue(value);
    
    settings.setValue("ovulacny/luteal",this->luteal);
    settings.setValue("ovulacny/ciel",this->ciel);
    settings.setValue("ovulacny/ZaciatokCykluDen",this->ZaciatokCykluDen);
    settings.setValue("ovulacny/ZaciatokCykluMesiac",this->ZaciatokCykluMesiac);
    settings.setValue("ovulacny/ZaciatokCykluRok",this->ZaciatokCykluRok);
    settings.sync();
    
    // nastavime hodnoty vo formulari nastavenia
    ui->ZaciatokCykluDen->setValue(this->ZaciatokCykluDen);
    ui->ZaciatokCykluMesiac->setValue(this->ZaciatokCykluMesiac);
    ui->ZaciatokCykluRok->setValue(this->ZaciatokCykluRok);
    
    ui->cyklus->setValue(this->cyklus);
    ui->luteal->setValue(this->luteal);
    
    if(this->ciel == 0)
    {
        ui->ciel1->setChecked(true);
        ui->ciel2->setChecked(false);
    }
    else
    {
        ui->ciel1->setChecked(false);
        ui->ciel2->setChecked(true);
    }
    
    this->SettingsLoaded = true;
    

    }

    void MainWindow::nastavitNastaveniaNaPredvolene()
    {
    return;
    // nastavime this hodnoty
    this->cyklus = 28;
    this->luteal = 14;
    this->ciel = 1;

    time_t cas; tm *str;         //vytvorenie pointru typu tm
    time(&cas);
    
    str = localtime(&cas);
    
    this->ZaciatokCykluDen = str->tm_mday;
    this->ZaciatokCykluMesiac = str->tm_mon;
    this->ZaciatokCykluRok = str->tm_year;
    
    // ulozime hodnoty
    this->nastavitNastavenia();
    

    }

    bool MainWindow::nacitatNastavenia()
    {
    QSettings settings("ovulacnykalendar");

    this->cyklus = settings.value("ovulacny/cyklus",24).toInt();
    this->luteal = settings.value("ovulacny/luteal").toInt();
    this->ciel = settings.value("ovulacny/ciel").toInt();
    
    this->ZaciatokCykluDen = settings.value("ovulacny/ZaciatokCykluDen").toInt();
    this->ZaciatokCykluMesiac = settings.value("ovulacny/ZaciatokCykluMesiac",11).toInt();
    this->ZaciatokCykluRok = settings.value("ovulacny/ZaciatokCykluRok",2010).toInt();
    
    bool returning = settings.contains("ovulacny/cyklus");
    
    settings.sync();
    return returning;
    

    }

    void MainWindow::ulozitNastavenia(int value)
    {
    if(!this->SettingsLoaded)
    return;

    QSettings settings("ovulacnykalendar");
    
    this->cyklus = ui->cyklus->value();
    this->luteal = ui->luteal->value();
    
    if(ui->ciel1->isChecked())
    {
        this->ciel = 0;
        settings.setValue("ovulacny/ciel",0);
    }
    
    if(ui->ciel2->isChecked())
    {
        this->ciel = 1;
         settings.setValue("ovulacny/ciel",1);
    }
    
    this->ZaciatokCykluMesiac = ui->ZaciatokCykluMesiac->value();
    this->ZaciatokCykluDen = ui->ZaciatokCykluDen->value();
    this->ZaciatokCykluRok = ui->ZaciatokCykluRok->value();
    
    settings.setValue("ovulacny/cyklus",ui->cyklus->value());
    
    settings.setValue("ovulacny/luteal",ui->luteal->value());
    
    settings.setValue("ovulacny/ZaciatokCykluDen",ui->ZaciatokCykluDen->value());
    settings.setValue("ovulacny/ZaciatokCykluMesiac",ui->ZaciatokCykluMesiac->value());
        ui->cyklus->setValue(settings.value("ovulacny/ZaciatokCykluMesiac").toInt());
    
        settings.setValue("ovulacny/ZaciatokCykluRok",ui->ZaciatokCykluRok->value());
    settings.sync();
    

    }
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.