Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Saving dial value
Forum Updated to NodeBB v4.3 + New Features

Saving dial value

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 3 Posters 7.3k Views 2 Watching
  • 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.
  • R Offline
    R Offline
    rapid84
    wrote on last edited by
    #1

    Hi,
    In my project, there is a second.ui apart from Mainwindow and it has a dial. I set the dial value to a custom value and i close second.ui. when i reopen second ui, it does not save the value that i set before i close it. So i have define a value in "second.h"

    int save ;
    

    Then i write the dial value to "save" before closing the ui;

    void second::on_closeButton_clicked()
    {
        save = ui->dial->value();
        close();
    }
    

    When i reopen the secon.ui , i read the save value and set the dial ;

    ui->setupUi(this);   
    ui->dial->setValue(save);
    

    But i does not set the rigth value , it set the maximum dial value. Where is the error ?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      "it set the maximum dial value."

      That sounds odd. You mean that it goes to max value when u set it ?

      Is the save value what you expect if u
      place break point and look at it ?
      or you can
      qDebug() << " save val:" << save;
      before the
      ui->dial->setValue(save);

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rapid84
        wrote on last edited by
        #3

        @mrjj said:

        qDebug() << " save val:" << save;

        when i do qDebug i get this value;

        save val: 1991605744
        

        But when i save it, it was between 0 and 100 ?

        mrjjM 1 Reply Last reply
        0
        • R rapid84

          @mrjj said:

          qDebug() << " save val:" << save;

          when i do qDebug i get this value;

          save val: 1991605744
          

          But when i save it, it was between 0 and 100 ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @rapid84
          Well, can you show how u save and load it. ?

          it seems to be loaded wrong maybe, becoming huge or
          could also look uninitialized.

          R 1 Reply Last reply
          0
          • mrjjM mrjj

            @rapid84
            Well, can you show how u save and load it. ?

            it seems to be loaded wrong maybe, becoming huge or
            could also look uninitialized.

            R Offline
            R Offline
            rapid84
            wrote on last edited by
            #5

            @mrjj

            when i save it and do qDebug like this;

            void second::on_closeButton_clicked()
            {
                save = ui->dial->value();
               qDebug() << " save val:" << save;
                close();
            }
            

            i got the rigth value from qDebug . But at startup ;

            .....
            ui->setupUi(this);   
            ui->dial->setValue(save);
            qDebug() << " save val:" << save;
            

            i got wrong value.

            1 Reply Last reply
            0
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              ok, but how to you save it to file ? and load it from file again?

              If you dont load the old value from a file, it will
              just have some random value when you start again.

              R 1 Reply Last reply
              0
              • mrjjM mrjj

                ok, but how to you save it to file ? and load it from file again?

                If you dont load the old value from a file, it will
                just have some random value when you start again.

                R Offline
                R Offline
                rapid84
                wrote on last edited by
                #7

                @mrjj
                yow want to know whether i use QFile or something same to store the value or not ? if so , i dont use anything like that. İ just define save variable in second.h module with default value "0".

                mrjjM 1 Reply Last reply
                0
                • R rapid84

                  @mrjj
                  yow want to know whether i use QFile or something same to store the value or not ? if so , i dont use anything like that. İ just define save variable in second.h module with default value "0".

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @rapid84
                  Ok. but it cannot remember values from run to run, unless you save it in a file.
                  I do wonder why it has that high value if you set it to 0.

                  R 1 Reply Last reply
                  0
                  • mrjjM mrjj

                    @rapid84
                    Ok. but it cannot remember values from run to run, unless you save it in a file.
                    I do wonder why it has that high value if you set it to 0.

                    R Offline
                    R Offline
                    rapid84
                    wrote on last edited by
                    #9

                    @mrjj

                    if i define with "0" i got only "0" value, but when i define without any default, i got some value like "199922356" , so it is just misunderstanding. anyway, then i have to use QFile, crate a file and save the value to it, and load the save value from a file ?

                    mrjjM 1 Reply Last reply
                    0
                    • R rapid84

                      @mrjj

                      if i define with "0" i got only "0" value, but when i define without any default, i got some value like "199922356" , so it is just misunderstanding. anyway, then i have to use QFile, crate a file and save the value to it, and load the save value from a file ?

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @rapid84
                      If you dont define any default value, it will just have random value as it just uses
                      what ever is in that memory location.

                      Yes, to keep it for later runs, you must save it to file. QFile is fine to use.
                      You can also have a look at QSettings.
                      http://doc.qt.io/qt-5/qsettings.html
                      Its ment for saving such values.

                      its easy to use

                      //load
                       QSettings settings("settingName");
                       int  aValue=settings.value("aKey").toInt();
                      
                       //write
                       QSettings settings("settingName");
                      int somevalue=yoursave;
                       settings.setValue("aKey",somevalue);
                      
                      R 1 Reply Last reply
                      0
                      • mrjjM mrjj

                        @rapid84
                        If you dont define any default value, it will just have random value as it just uses
                        what ever is in that memory location.

                        Yes, to keep it for later runs, you must save it to file. QFile is fine to use.
                        You can also have a look at QSettings.
                        http://doc.qt.io/qt-5/qsettings.html
                        Its ment for saving such values.

                        its easy to use

                        //load
                         QSettings settings("settingName");
                         int  aValue=settings.value("aKey").toInt();
                        
                         //write
                         QSettings settings("settingName");
                        int somevalue=yoursave;
                         settings.setValue("aKey",somevalue);
                        
                        R Offline
                        R Offline
                        rapid84
                        wrote on last edited by
                        #11

                        @mrjj

                        "aKey" is a mandatory paramater ? and the reference mentions that:

                        When creating a QSettings object, you must pass the name of your company or organization as well as the name of your application. For example, if your product is called Star Runner and your company is called MySoft, you would construct the QSettings object as follows:
                        
                            QSettings settings("MySoft", "Star Runner");
                        

                        Is this mandatory ? what if , if i dont have any company ? :)

                        mrjjM 1 Reply Last reply
                        0
                        • R rapid84

                          @mrjj

                          "aKey" is a mandatory paramater ? and the reference mentions that:

                          When creating a QSettings object, you must pass the name of your company or organization as well as the name of your application. For example, if your product is called Star Runner and your company is called MySoft, you would construct the QSettings object as follows:
                          
                              QSettings settings("MySoft", "Star Runner");
                          

                          Is this mandatory ? what if , if i dont have any company ? :)

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @rapid84
                          hi
                          the akey is the name of the value. It saves to registry so that why it needs a name
                          For you , it could be slidervalue or something that explains what u save.

                          You can use anything u like for company. Its also because its store in registry.
                          So just use some name u like that IS not a real company. :)

                          R 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @rapid84
                            hi
                            the akey is the name of the value. It saves to registry so that why it needs a name
                            For you , it could be slidervalue or something that explains what u save.

                            You can use anything u like for company. Its also because its store in registry.
                            So just use some name u like that IS not a real company. :)

                            R Offline
                            R Offline
                            rapid84
                            wrote on last edited by
                            #13

                            @mrjj

                            For loading i use;

                             QSettings settings("settingName");
                                int  aValue=settings.value("aKey").toInt();
                                int  aValue2=settings.value("aKey2").toInt();
                                ui->dial->setValue(aValue);
                                ui->dial_2->setValue(aValue2);
                            

                            and for saving i use

                            QSettings settings("settingName");
                                int somevalue = ui->dial->value();
                                int somevalue2 = ui->dial_2->value();
                                settings.setValue("aKey",somevalue);
                                settings.setValue("aKey2",somevalue2);
                            

                            These work for me , thanks for help. by the way, in a debian OS , where do it save ( in which directory)?

                            1 Reply Last reply
                            1
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Hi
                              That is good question :)
                              try
                              qDebug() << settings.fileName();

                              1 Reply Last reply
                              0
                              • mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                If I may suggest one improvement is to use a better name than
                                "aKey" and "aKey2"
                                Maybe Dial1Value and Dial2Value

                                R 1 Reply Last reply
                                0
                                • mrjjM mrjj

                                  If I may suggest one improvement is to use a better name than
                                  "aKey" and "aKey2"
                                  Maybe Dial1Value and Dial2Value

                                  R Offline
                                  R Offline
                                  rapid84
                                  wrote on last edited by rapid84
                                  #16

                                  @mrjj

                                  when i click the close button, while it is saving data , it waits sometime and close with a 1-2 seconds delay. Is this normal ?

                                  mrjjM 1 Reply Last reply
                                  0
                                  • R rapid84

                                    @mrjj

                                    when i click the close button, while it is saving data , it waits sometime and close with a 1-2 seconds delay. Is this normal ?

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @rapid84
                                    Normally it saves really fast so i never noticed it.

                                    R 1 Reply Last reply
                                    0
                                    • mrjjM mrjj

                                      @rapid84
                                      Normally it saves really fast so i never noticed it.

                                      R Offline
                                      R Offline
                                      rapid84
                                      wrote on last edited by
                                      #18

                                      @mrjj
                                      on raspbian sometimes it takes up to 5 -6 seconds to store the data. i have tested it, maybe i will need to use QFile.

                                      mrjjM 1 Reply Last reply
                                      0
                                      • R rapid84

                                        @mrjj
                                        on raspbian sometimes it takes up to 5 -6 seconds to store the data. i have tested it, maybe i will need to use QFile.

                                        mrjjM Offline
                                        mrjjM Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #19

                                        @rapid84
                                        ok. thats pretty long.
                                        It must have a reason.
                                        QFile should be faster.

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          Hi,

                                          You shouldn't need to but you can try adding a call to sync in order to flush the content of your settings to the disk.

                                          Interested in AI ? www.idiap.ch
                                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          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