Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. [Solved] Qsettings dont save the values

[Solved] Qsettings dont save the values

Scheduled Pinned Locked Moved Mobile and Embedded
17 Posts 4 Posters 11.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tristenn
    wrote on last edited by
    #8

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #9

      [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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tristenn
        wrote on last edited by
        #10

        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.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #11

          [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]

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • T Offline
            T Offline
            tristenn
            wrote on last edited by
            #12

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

            Errors about qDebug like invalid use of incomlete type struct Qdebug

            1 Reply Last reply
            0
            • S Offline
              S Offline
              stukdev
              wrote on last edited by
              #13

              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]

              1 Reply Last reply
              0
              • M Offline
                M Offline
                milot.shala
                wrote on last edited by
                #14

                [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.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  milot.shala
                  wrote on last edited by
                  #15

                  [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;
                  }

                  @

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #16

                    [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.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • T Offline
                      T Offline
                      tristenn
                      wrote on last edited by
                      #17

                      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();
                      

                      }
                      @

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved