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. QSettings uncorrect saving
Qt 6.11 is out! See what's new in the release blog

QSettings uncorrect saving

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.2k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by A Former User
    #1

    if call method for reading settings after method for saving,while program working,then QMap saved.But if restart program, then settings don't saving

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtWidgets>
    #include "mainnotes.h"
    #include <QtCore>

    typedef QMap<int,MainNotes*> QMapNotes;
    Q_DECLARE_METATYPE(QMapNotes)

    class MainWindow : public QWidget
    {
    Q_OBJECT

    private:
    //QMap<long,MainNotes*> localeforNotes;
    QMapNotes localeforNotes;
    QMap<long,QString> proxyLocal;

         long idNote;
         QString mainThemeNote;
    
         QPushButton *createButton;
         QPushButton *deleteButton;
         QPushButton *changeButton;
    
         QBoxLayout  *buttonLayout;
         QHBoxLayout *horizontalLayout;
         QVBoxLayout *verticalLayout;
         MainNotes *newNote;
         QSplitter *mainNoteSplitter;
         QSettings mainSettings;
    
         void createButtonClick();
         void deleteButtonClick();
         void changeButtonClick();
    

    public:
    explicit MainWindow(QWidget *parent = 0);
    virtual ~MainWindow();

         void readSettings();
         void writeSettings();
    

    signals:

    public slots:
    void deleteNote();
    void changeSettings();
    void createNote();
    void buttonActivate();

    };

    static QDataStream &operator>>(QDataStream &in, QMapNotes &notes)
    {

    return in;
    

    }

    #endif // MAINWINDOW_H

    //////////////////////////////
    mainwindow.cpp

    ////////// In constructor of class

    qRegisterMetaType<QMapNotes>("QMapNotes");
    qRegisterMetaTypeStreamOperators<QMapNotes>("QMapNotes");
    

    /////////
    ///// method for saving
    void MainWindow::writeSettings()
    {

    mainSettings.beginGroup("/Settings");

     mainSettings.setValue("/notes",QVariant::fromValue<QMapNotes>(localeforNotes));
    

    mainSettings.endGroup();
    }

    ///method for reading settings

    void MainWindow::readSettings()
    {
    mainSettings.beginGroup("/Settings");

    QVariant savingSettings = mainSettings.value("/notes");
    

    localeforNotes = savingSettings.value<QMapNotes>();

    QMapNotes::iterator it = localeforNotes.begin();

     while(it != localeforNotes.end())
       {
         mainNoteSplitter->insertWidget(it.key(),it.value());
    
         it++;
       }
    
    mainSettings.endGroup();
    

    }

    ? 1 Reply Last reply
    0
    • ? A Former User

      if call method for reading settings after method for saving,while program working,then QMap saved.But if restart program, then settings don't saving

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H

      #include <QtWidgets>
      #include "mainnotes.h"
      #include <QtCore>

      typedef QMap<int,MainNotes*> QMapNotes;
      Q_DECLARE_METATYPE(QMapNotes)

      class MainWindow : public QWidget
      {
      Q_OBJECT

      private:
      //QMap<long,MainNotes*> localeforNotes;
      QMapNotes localeforNotes;
      QMap<long,QString> proxyLocal;

           long idNote;
           QString mainThemeNote;
      
           QPushButton *createButton;
           QPushButton *deleteButton;
           QPushButton *changeButton;
      
           QBoxLayout  *buttonLayout;
           QHBoxLayout *horizontalLayout;
           QVBoxLayout *verticalLayout;
           MainNotes *newNote;
           QSplitter *mainNoteSplitter;
           QSettings mainSettings;
      
           void createButtonClick();
           void deleteButtonClick();
           void changeButtonClick();
      

      public:
      explicit MainWindow(QWidget *parent = 0);
      virtual ~MainWindow();

           void readSettings();
           void writeSettings();
      

      signals:

      public slots:
      void deleteNote();
      void changeSettings();
      void createNote();
      void buttonActivate();

      };

      static QDataStream &operator>>(QDataStream &in, QMapNotes &notes)
      {

      return in;
      

      }

      #endif // MAINWINDOW_H

      //////////////////////////////
      mainwindow.cpp

      ////////// In constructor of class

      qRegisterMetaType<QMapNotes>("QMapNotes");
      qRegisterMetaTypeStreamOperators<QMapNotes>("QMapNotes");
      

      /////////
      ///// method for saving
      void MainWindow::writeSettings()
      {

      mainSettings.beginGroup("/Settings");

       mainSettings.setValue("/notes",QVariant::fromValue<QMapNotes>(localeforNotes));
      

      mainSettings.endGroup();
      }

      ///method for reading settings

      void MainWindow::readSettings()
      {
      mainSettings.beginGroup("/Settings");

      QVariant savingSettings = mainSettings.value("/notes");
      

      localeforNotes = savingSettings.value<QMapNotes>();

      QMapNotes::iterator it = localeforNotes.begin();

       while(it != localeforNotes.end())
         {
           mainNoteSplitter->insertWidget(it.key(),it.value());
      
           it++;
         }
      
      mainSettings.endGroup();
      

      }

      ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Seems you are storing pointers to your MainNotes and your QDataStream operator>> doesn't do anything. That won't help much on the save/load part.

        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
        • SGaistS SGaist

          Hi,

          Seems you are storing pointers to your MainNotes and your QDataStream operator>> doesn't do anything. That won't help much on the save/load part.

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by A Former User
          #4

          @SGaist I am sorry How i can do correctly save of QMap with my QTextEdit's objects,that if program restart that i have QMap with objects inside?

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

            You must provide both operators for your custom type then you need to use Q_DECLARE_METATYPE to make it known and qRegisterMetaTypeStreamOperators to make the stream operators known. You will then be able to store and load your class.

            You don't need to do anything particular for QMap, it's already handled for you however your must also call qRegisterMetaTypeStreamOperators for your QMapNotes (note that you shouldn't prefix your classes with Q, that will make thing confusing for other people reading your code)

            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
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Okay,I am understand,but qRegisterMetaTypeStreamOperators I must using for input or output my QMap,right?

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

                qRegisterMetaTypeStreamOperators register both operators. You will need both

                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
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  Ok,Thx, I try to do it

                  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