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. How To Save And Load A Qt Window to file?
Forum Updated to NodeBB v4.3 + New Features

How To Save And Load A Qt Window to file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 1.5k Views 3 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.
  • J javad_2012
    2 Dec 2021, 15:24

    Hi everyone. I am new to Qt(maybe use wrong terms). I have a window(a form) with some text editors and labels and radio buttons and ... . I use this page for setting and the number of tabs in this window is very lot.
    How I can save all of this window at once and load it at once on the other time? I saw some examples of saving and loading objects and classes into the file but they don't work for me. Because I haven't string and integer or float. I have a window with some complex objects and some pointers. I attached a picture of my window.
    I am reading about it for a couple of days but I can't find the solution.
    Thanks a lot for your attention to help me.

    sample.PNG

    A Offline
    A Offline
    artwaw
    wrote on 2 Dec 2021, 15:39 last edited by artwaw 12 Feb 2021, 15:40
    #3

    @javad_2012 To add to what @JonB said, you can save the geometry of the window, state of some widgets and such - which together with a proper use of layouts (and saved actual values presented in the widgets) should achieve what you ask.

    Please take a look at QSettings and QDataStream - they serve different purposes but at this moment it is unclear to me what exactly you need.

    EDIT: you can actually use a rule of thumb here: for saving program parameters, you will find QSettings more useful, for actual program data - QDataStream or Sqlite db.

    For more information please re-read.

    Kind Regards,
    Artur

    1 Reply Last reply
    2
    • J javad_2012
      2 Dec 2021, 15:24

      Hi everyone. I am new to Qt(maybe use wrong terms). I have a window(a form) with some text editors and labels and radio buttons and ... . I use this page for setting and the number of tabs in this window is very lot.
      How I can save all of this window at once and load it at once on the other time? I saw some examples of saving and loading objects and classes into the file but they don't work for me. Because I haven't string and integer or float. I have a window with some complex objects and some pointers. I attached a picture of my window.
      I am reading about it for a couple of days but I can't find the solution.
      Thanks a lot for your attention to help me.

      sample.PNG

      P Offline
      P Offline
      Pl45m4
      wrote on 2 Dec 2021, 16:48 last edited by Pl45m4 12 Feb 2021, 16:49
      #4

      @javad_2012

      You could also save to data to a JSON array or maybe some sort of XML style file. It looks like you have a bunch of Key/Value pairs.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      A 1 Reply Last reply 2 Dec 2021, 16:57
      0
      • P Pl45m4
        2 Dec 2021, 16:48

        @javad_2012

        You could also save to data to a JSON array or maybe some sort of XML style file. It looks like you have a bunch of Key/Value pairs.

        A Offline
        A Offline
        artwaw
        wrote on 2 Dec 2021, 16:57 last edited by
        #5

        @Pl45m4 said in How To Save And Load A Qt Window to file?:

        Key/Value pairs.

        QSettings, no need to play around with JSON (it is cumbersome at best).

        For more information please re-read.

        Kind Regards,
        Artur

        1 Reply Last reply
        1
        • J Offline
          J Offline
          javad_2012
          wrote on 3 Dec 2021, 10:10 last edited by
          #6

          @JonB @artwaw @Pl45m4 thanks for your answers.
          But the problem is all of the tabs in my window are dynamic. For example, if you look at the picture, in the left, when you choose a Mash, in the general tab you have a Drop-down widget. By selecting an item from that Drop-down all of the tabs and items in other tabs on the left like solver setup and properties and solution setup and... will be changed. We have about 150 tables in the DB and on average each of them has 80 rows and all of them are for just this window. If I want to save the entities of items the user was chosen for each tab, the probabilistic of that items will be very high and it's really huge particular in load.
          I read about Qsetting but as I understand it Qsetting is good for storing the properties of your entities like position, width, height and, so on. But here I need positions and values for everything.

          J 1 Reply Last reply 3 Dec 2021, 11:24
          0
          • J javad_2012
            3 Dec 2021, 10:10

            @JonB @artwaw @Pl45m4 thanks for your answers.
            But the problem is all of the tabs in my window are dynamic. For example, if you look at the picture, in the left, when you choose a Mash, in the general tab you have a Drop-down widget. By selecting an item from that Drop-down all of the tabs and items in other tabs on the left like solver setup and properties and solution setup and... will be changed. We have about 150 tables in the DB and on average each of them has 80 rows and all of them are for just this window. If I want to save the entities of items the user was chosen for each tab, the probabilistic of that items will be very high and it's really huge particular in load.
            I read about Qsetting but as I understand it Qsetting is good for storing the properties of your entities like position, width, height and, so on. But here I need positions and values for everything.

            J Offline
            J Offline
            JonB
            wrote on 3 Dec 2021, 11:24 last edited by JonB 12 Mar 2021, 11:56
            #7

            @javad_2012 said in How To Save And Load A Qt Window to file?:

            If I want to save the entities of items the user was chosen for each tab, the probabilistic of that items will be very high and it's really huge particular in load.

            You have no choice! If the user has altered whatever then by definition you need to save it in some shape or form if you are going to restore it later. And it would make no difference if you could somehow save the widgets, you would still have the same amount of information to save; in fact, if you could store full widget state, trust me, it would end up being a lot larger!

            It is your job to figure just what/how much you need to save in order to fully restore your desired state. You can then think about reducing that to a minimum size if that bothers you.

            If the database content you re showing can be reproduced at a later date by a suitable query against the database then you do not need to save that data (it's already in whatever other database). But you would need to save, say, information about a choice the user has made in a combobox to filter the data if you want to restore that later.

            If you have a fair amount of data to save I would suggest not doing so in QSettings --- although technically you could. Other possibilities might include: create and save JSON (or QDataStream) to a file, or use perhaps a SQLite file database.

            A 1 Reply Last reply 3 Dec 2021, 11:54
            4
            • J JonB
              3 Dec 2021, 11:24

              @javad_2012 said in How To Save And Load A Qt Window to file?:

              If I want to save the entities of items the user was chosen for each tab, the probabilistic of that items will be very high and it's really huge particular in load.

              You have no choice! If the user has altered whatever then by definition you need to save it in some shape or form if you are going to restore it later. And it would make no difference if you could somehow save the widgets, you would still have the same amount of information to save; in fact, if you could store full widget state, trust me, it would end up being a lot larger!

              It is your job to figure just what/how much you need to save in order to fully restore your desired state. You can then think about reducing that to a minimum size if that bothers you.

              If the database content you re showing can be reproduced at a later date by a suitable query against the database then you do not need to save that data (it's already in whatever other database). But you would need to save, say, information about a choice the user has made in a combobox to filter the data if you want to restore that later.

              If you have a fair amount of data to save I would suggest not doing so in QSettings --- although technically you could. Other possibilities might include: create and save JSON (or QDataStream) to a file, or use perhaps a SQLite file database.

              A Offline
              A Offline
              artwaw
              wrote on 3 Dec 2021, 11:54 last edited by
              #8

              @JonB I concur (although I still despise using JSON for that). Your use case, assuming you read and write one by one in determined series (very important assumption) fits QDataStream, otherwise - if want to be able to pick any value from the dataset at will - indeed JSON.

              For more information please re-read.

              Kind Regards,
              Artur

              J 1 Reply Last reply 3 Dec 2021, 12:02
              0
              • A artwaw
                3 Dec 2021, 11:54

                @JonB I concur (although I still despise using JSON for that). Your use case, assuming you read and write one by one in determined series (very important assumption) fits QDataStream, otherwise - if want to be able to pick any value from the dataset at will - indeed JSON.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 3 Dec 2021, 12:02 last edited by
                #9
                This post is deleted!
                A 1 Reply Last reply 3 Dec 2021, 12:29
                0
                • J jsulm
                  3 Dec 2021, 12:02

                  This post is deleted!

                  A Offline
                  A Offline
                  artwaw
                  wrote on 3 Dec 2021, 12:29 last edited by
                  #10

                  @jsulm not what I intended, just to save values in a determined sequence. Recreating the widgets is a matter of code, layouts, possibly saved states of some of them.

                  For more information please re-read.

                  Kind Regards,
                  Artur

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    javad_2012
                    wrote on 6 Dec 2021, 11:44 last edited by
                    #11

                    @JonB
                    @artwaw
                    thanks for your help.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mpergand
                      wrote on 6 Dec 2021, 12:35 last edited by
                      #12

                      The major drawback with QDataStream is that you can’t change anything as the stream becomes invalid in an instant. In a revision of the prog if you add/remove even a single parameter, you need to deal with different versions of the saved data.
                      Cumbersome and error prone …

                      There’s an alternative: Keyed archiver

                      void ExcludedSite::archiveWithArchiver(KeyedArchiver &ar) const
                      {
                         ar["url"]=aUrl;
                         ar["root"]=bRoot;
                      }
                      
                      bool ExcludedSite::unArchiveWithArchiver(const KeyedArchiver &ar)
                      {
                          aUrl=ar["url"].toUrl();
                          bRoot=ar["root"].toBool();
                      
                          return true;
                      }
                      

                      KeyArchiver is just an alias for QHash<QString,QVariant>

                      Easy to implement and if a parameter doesn’t exist, hash return a default value/object for it, easy to deal with.

                      You can thing of it as some kind of binary/serialized JSON

                      A 1 Reply Last reply 6 Dec 2021, 13:28
                      0
                      • M mpergand
                        6 Dec 2021, 12:35

                        The major drawback with QDataStream is that you can’t change anything as the stream becomes invalid in an instant. In a revision of the prog if you add/remove even a single parameter, you need to deal with different versions of the saved data.
                        Cumbersome and error prone …

                        There’s an alternative: Keyed archiver

                        void ExcludedSite::archiveWithArchiver(KeyedArchiver &ar) const
                        {
                           ar["url"]=aUrl;
                           ar["root"]=bRoot;
                        }
                        
                        bool ExcludedSite::unArchiveWithArchiver(const KeyedArchiver &ar)
                        {
                            aUrl=ar["url"].toUrl();
                            bRoot=ar["root"].toBool();
                        
                            return true;
                        }
                        

                        KeyArchiver is just an alias for QHash<QString,QVariant>

                        Easy to implement and if a parameter doesn’t exist, hash return a default value/object for it, easy to deal with.

                        You can thing of it as some kind of binary/serialized JSON

                        A Offline
                        A Offline
                        artwaw
                        wrote on 6 Dec 2021, 13:28 last edited by
                        #13

                        @mpergand Yes however, if you read QDataStream docs, versioning of the data is advised, so it is not THAT terrible. Main problem I see is sequential access to the data.

                        For more information please re-read.

                        Kind Regards,
                        Artur

                        1 Reply Last reply
                        0
                        • M mpergand referenced this topic on 27 Jul 2023, 14:07
                        • M mpergand referenced this topic on 9 Dec 2023, 20:11

                        12/13

                        6 Dec 2021, 12:35

                        • Login

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