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?

How To Save And Load A Qt Window to file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 6 Posters 1.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.
  • J Offline
    J Offline
    javad_2012
    wrote on last edited by
    #1

    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

    JonBJ artwawA Pl45m4P 3 Replies Last reply
    0
    • J javad_2012

      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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @javad_2012
      The simple answer is you cannot save/load any kind of QWidget. Widgets are not "serializable". Instead it would be up to you to save the values which are currently set in the widget --- perhaps by putting them in a model --- and then you must still recreate the widget (and any subwidgets) by whatever means yourself, then read the saved values and put them back into the widget. That is probably whatever you read elsewhere.

      1 Reply Last reply
      3
      • J javad_2012

        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

        artwawA Offline
        artwawA Offline
        artwaw
        wrote on last edited by artwaw
        #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

          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

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #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

          artwawA 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

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

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on 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 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.

              JonBJ 1 Reply Last reply
              0
              • J javad_2012

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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.

                artwawA 1 Reply Last reply
                4
                • JonBJ JonB

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

                  artwawA Offline
                  artwawA Offline
                  artwaw
                  wrote on 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

                  jsulmJ 1 Reply Last reply
                  0
                  • artwawA artwaw

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

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9
                    This post is deleted!
                    artwawA 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      This post is deleted!

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on 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 last edited by
                        #11

                        @JonB
                        @artwaw
                        thanks for your help.

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mpergand
                          wrote on 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

                          artwawA 1 Reply Last reply
                          0
                          • M mpergand

                            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

                            artwawA Offline
                            artwawA Offline
                            artwaw
                            wrote on 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
                            • M mpergand referenced this topic on

                            • Login

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