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] using QSettings
QtWS25 Last Chance

[solved] using QSettings

Scheduled Pinned Locked Moved Mobile and Embedded
18 Posts 3 Posters 7.7k 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.
  • A Offline
    A Offline
    alfah
    wrote on 22 Aug 2011, 13:29 last edited by
    #1

    hello everybody,

    Im learning how to save data and read them back to be displayed.
    I have used QSettings for this.
    The text in a line edit is to be saved to a register and then display it when the class loads again.
    for writing it to the register ive used the following code

    @
    settings.beginWriteArray("notesList");
    reg.note=ui->lineEdit->text();
    notesList.append(reg);
    for(int i=0;i<notesList.size();++i)
    {
    settings.setArrayIndex(i);
    settings.setValue("note",notesList.at(i).note);
    }

    for (int i = 0; i < notesList.size(); ++i)
    {
        settings.setArrayIndex(i);
        settings.setValue("note", notesList.at(i).note);
        //settings.setValue("dtHistoryDate", lDateRegistryList.at(i).dtHistoryReg);
    }
    
    settings.endArray();
    

    @

    and for reading it back, ive written the following code in the constructor
    @
    int sizeReg = settings.beginReadArray("notesList");

    for(int i = 0; i < sizeReg;i++)
    {
        settings.setArrayIndex(i);
        reg.note = settings.value("note").toString();
        ui->lineEdit->setText(reg.note);  
    }
    

    @

    Somehow this code does not work in the simulator. I cant even enter the text

    help :(

    alfah

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on 22 Aug 2011, 13:39 last edited by
      #2

      If you just want to save and restore data I would suggest you use "QDataStream":http://doc.qt.nokia.com/latest/qdatastream.html instead, which allows for serializing almost any Qt data structure and container in a single line of code.

      Besides that - is there a reason you store your values twice? Does storing any other value using QSettings work?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        alfah
        wrote on 22 Aug 2011, 13:51 last edited by
        #3

        lukas,

        Could you explain to me how both are different and under which situations these are used???

        The module of mine requires notes to be attached to each date of a custom made calender. So when i click the button, i get another form with a line edit askin the user to input any notes if needed
        I would need to attach the date to each note too.

        alfah

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alfah
          wrote on 23 Aug 2011, 07:19 last edited by
          #4

          somebody tell me why the following code does not set value :(
          @
          settings.setValue("note",notesList.at(i).note);
          @

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Robbin
            wrote on 23 Aug 2011, 07:32 last edited by
            #5

            @
            reg.note=ui->lineEdit->text();
            notesList.append(reg);
            @

            What is the declaration of "reg" and "noteList"?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alfah
              wrote on 23 Aug 2011, 07:40 last edited by
              #6

              @
              struct registryStrct
              {
              QString note;
              // QDate clickedDate;

              };registryStrct reg;
              
              QList<registryStrct>notesList;
              

              @

              I debugged the program and found the following

              @
              Dialog::Dialog(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::Dialog)
              {
              ui->setupUi(this);

              // it shows the notesList size as zero!! this is the problem!!

              settings.beginReadArray("notesList");
              qDebug()<<"text "<<notesList.size();
              for(int i = 0; i < notesList.size() ;++i)
              {
                  settings.setArrayIndex(i);
                  reg.note = settings.value("note").toString();
                  qDebug()<<"text "<<reg.note;
                  ui->lineEdit->setText(reg.note);
              
              }
              settings.endArray();
              

              }

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

              void Dialog::on_pushButton_clicked()
              {

              // Write data to registry
              
              settings.beginWriteArray("notesList");
              
              reg.note=ui->lineEdit->text();
              qDebug()<<"hello"<<reg.note;
              notesList.append(reg);
              qDebug()<<"hello 2"<<notesList.at(0).note ;
              qDebug()<<"hello 3"<<notesList.size();
              for(int i=0;i<notesList.size();++i)
              {
                  qDebug()<<"hello 4"<<notesList.size();
                  settings.setArrayIndex(i);
                  settings.setValue("note",notesList.at(i).note);
                  qDebug()<<"hello 5"<< settings.value("note").toString();
              }
              
              settings.endArray();
              
              MainWindow *w= new MainWindow();
              w->showFullScreen();
              

              }
              @

              It saves the data , cannot reproduce it

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Robbin
                wrote on 23 Aug 2011, 07:50 last edited by
                #7

                Yes, it IS zero size, that's expected behaviour. You have error in your syntax. From the documentation:
                @
                QSettings settings;
                int size = settings.beginReadArray("logins");
                for (int i = 0; i < size; ++i) {
                @

                and you do:
                @
                settings.beginReadArray("notesList");
                qDebug()<<"text "<<notesList.size();
                for(int i = 0; i < notesList.size() ;++i)
                @

                Get the size in integer variable and use that for the loop.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  alfah
                  wrote on 23 Aug 2011, 08:46 last edited by
                  #8

                  ahhh k :)

                  mercii :D :D

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    alfah
                    wrote on 23 Aug 2011, 11:27 last edited by
                    #9

                    one more question,

                    an applications can have as many settings as possible right?

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      Robbin
                      wrote on 23 Aug 2011, 11:36 last edited by
                      #10

                      Well you have groups. Just divide your settings in appropriate groups. You can have many settings, and QSettings retrieves the data for you. I don't know about limitation in size.

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        alfah
                        wrote on 23 Aug 2011, 12:06 last edited by
                        #11

                        ok,
                        somethin else, its said tht qsettings can hold all sort of settings right?
                        I have entered stored a date like this
                        @
                        noteSettings.setValue("setDate",notesList.at(i).setNoteDate);
                        @

                        reg.setNoteDate is of QDate type
                        while reading it back, do we need it to use .toDate()
                        like this??
                        @
                        QDate checkDate=noteSettings.value("setNoteDate").toDate();
                        @

                        thank you.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          Robbin
                          wrote on 23 Aug 2011, 12:19 last edited by
                          #12

                          yes, there is .toDate() method for QVariant (that's what gets returned by settings.value() method).

                          The only trouble you could get with QSettings is if you use GUI types like QColor.
                          Read "here":http://doc.qt.nokia.com/stable/qsettings.html for more info

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            lgeyer
                            wrote on 23 Aug 2011, 13:43 last edited by
                            #13

                            [quote author="alfah" date="1314021081"]lukas,
                            Could you explain to me how both are different and under which situations these are used???[/quote]

                            The main differences are

                            • sequential access for QDataStream vs random access for QSettings
                            • (efficient) binary storage for QDataStream vs text storage for QSettings
                            • non-human-readable storage for QDataStream vs human-readable storage for QSettings

                            There is no generally accepted rule on usage, but there is a good rule of thumb. QSettings is used to store the state of an application (opened windows, geometry) and settings (credentials, color schemes), whereas the data of an application itself is serialized using QDataStream (or another data storage like QSqlDatabase, QXmlStreamWriter).

                            [quote author="alfah" date="1314021081"]The module of mine requires notes to be attached to each date of a custom made calender. So when i click the button, i get another form with a line edit askin the user to input any notes if ...[/quote]

                            Have you considered storing the data using the "iCal standard":http://tools.ietf.org/html/rfc5545? This way your data will be interchangeable with many other applications processing calendar data.

                            1 Reply Last reply
                            0
                            • A Offline
                              A Offline
                              alfah
                              wrote on 23 Aug 2011, 13:47 last edited by
                              #14

                              does QdataStream provide persistent storage?

                              1 Reply Last reply
                              0
                              • R Offline
                                R Offline
                                Robbin
                                wrote on 23 Aug 2011, 13:49 last edited by
                                #15

                                Of course, you "stream" the data into a file.

                                1 Reply Last reply
                                0
                                • L Offline
                                  L Offline
                                  lgeyer
                                  wrote on 23 Aug 2011, 13:59 last edited by
                                  #16

                                  Or any other QIODevice.

                                  1 Reply Last reply
                                  0
                                  • A Offline
                                    A Offline
                                    alfah
                                    wrote on 23 Aug 2011, 14:27 last edited by
                                    #17

                                    will the following lines of code ensure tht Where ever i use the Qsettings, it will open up the same settings files?
                                    @

                                    QSettings noteSettings("Plackal","Seecycles");
                                    noteSettings.setDefaultFormat(QSettings::IniFormat);
                                    noteSettings.setPath(QSettings::IniFormat,QSettings::UserScope,QCoreApplication::applicationDirPath());

                                    @

                                    alfah

                                    1 Reply Last reply
                                    0
                                    • L Offline
                                      L Offline
                                      lgeyer
                                      wrote on 23 Aug 2011, 20:40 last edited by
                                      #18

                                      Unless your applicationDirPath() doesn't change, yes.

                                      Keep in mind that you possibliy have no permission to write to the application directory. This is why you should prefer a user scope directory (like %APPDATA%) over the application directory.

                                      1 Reply Last reply
                                      0

                                      1/18

                                      22 Aug 2011, 13:29

                                      • Login

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