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. Qt Save application state
QtWS25 Last Chance

Qt Save application state

Scheduled Pinned Locked Moved Solved General and Desktop
qt widgetqt c++
6 Posts 3 Posters 1.6k 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.
  • S Offline
    S Offline
    sachinrd
    wrote on 19 Jun 2021, 12:12 last edited by
    #1

    Hello,
    I have a qt widgets applications and i have a lot of widgets and csv models which the user can modify. I want to store the state of the application meaning properties of various widgets(there are literally too many widgets) and also tableview models. How and where do i store this. If i need to store in a file, where should the file be located such that i dont have to change the path of the application in different OSs ?
    Thanks for any help in advance

    A 1 Reply Last reply 19 Jun 2021, 12:28
    0
    • S sachinrd
      19 Jun 2021, 12:12

      Hello,
      I have a qt widgets applications and i have a lot of widgets and csv models which the user can modify. I want to store the state of the application meaning properties of various widgets(there are literally too many widgets) and also tableview models. How and where do i store this. If i need to store in a file, where should the file be located such that i dont have to change the path of the application in different OSs ?
      Thanks for any help in advance

      A Offline
      A Offline
      artwaw
      wrote on 19 Jun 2021, 12:28 last edited by
      #2

      @sachinrd Hi,
      as for "where" that is rather simple question, as Qt provides transparent and cross platform approach to this via QSettings class.

      As for "how" - that all depends. Some examples are provided in the QSettings documentation, some others are in respective widgets documentation. You should look for saveState() and saveGeometry() methods.

      Please bear in mind that saving geometry is a tricky thing - Qt computes those for widgets based on layouts applied, so you should rather save the size of the main window only. As for states, that's different thing and would rather require you to carefully think over what you exactly want to restore afterwards, what can be restored by hand and what should not due to said automatisation.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sachinrd
        wrote on 19 Jun 2021, 13:11 last edited by
        #3

        Hi @artwaw . Thanks for the quick reply.
        I understand i can store widget states in QSettings. What about csv data which is present as a model in the application. i cant store model data in QSettings right ?
        And also I have multiple graphicsview scenes and multiple widgets in the scene. Wouldnt it be better to store all the widget properties as a json to a file, where i get to the freedom to have a proper structure ? And i dont see a save state method in most of the widgets/graphicsitems that i am using .

        A J 2 Replies Last reply 19 Jun 2021, 14:20
        0
        • S sachinrd
          19 Jun 2021, 13:11

          Hi @artwaw . Thanks for the quick reply.
          I understand i can store widget states in QSettings. What about csv data which is present as a model in the application. i cant store model data in QSettings right ?
          And also I have multiple graphicsview scenes and multiple widgets in the scene. Wouldnt it be better to store all the widget properties as a json to a file, where i get to the freedom to have a proper structure ? And i dont see a save state method in most of the widgets/graphicsitems that i am using .

          A Offline
          A Offline
          artwaw
          wrote on 19 Jun 2021, 14:20 last edited by
          #4

          @sachinrd It is your decision to make.

          QSettings, if you read the documentation, is not exactly designed to store such data as CSV sets. Technically there is nothing to stop you from doing that though. You can also switch from platform dependent location (registry, plist, etc) to INI format and save it wherever.
          If you think JSON then of course you'll have to write your own system of storage. Qt has a few classes that might help you to construct such objects.
          Or you can take a look at QDataStream and an overview of data serialisation in Qt.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • S sachinrd
            19 Jun 2021, 13:11

            Hi @artwaw . Thanks for the quick reply.
            I understand i can store widget states in QSettings. What about csv data which is present as a model in the application. i cant store model data in QSettings right ?
            And also I have multiple graphicsview scenes and multiple widgets in the scene. Wouldnt it be better to store all the widget properties as a json to a file, where i get to the freedom to have a proper structure ? And i dont see a save state method in most of the widgets/graphicsitems that i am using .

            J Offline
            J Offline
            JonB
            wrote on 19 Jun 2021, 14:44 last edited by JonB
            #5

            @sachinrd said in Qt Save application state:

            And i dont see a save state method in most of the widgets/graphicsitems that i am using .

            There isn't. Don't try to serialize actual widgets. You need to store the changed attributes via some kind of model. And use that to reconstruct the widgets when required.

            You can save that kind of state by whatever suits you, e.g. settings, JSON.

            1 Reply Last reply
            2
            • S Offline
              S Offline
              sachinrd
              wrote on 19 Jun 2021, 16:31 last edited by
              #6

              Understood. Thanks for your responses @artwaw and @JonB .

              1 Reply Last reply
              0

              4/6

              19 Jun 2021, 14:20

              • Login

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