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. Howto save state of a specific widget?
Forum Updated to NodeBB v4.3 + New Features

Howto save state of a specific widget?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 5 Posters 10.3k Views 1 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.
  • S Offline
    S Offline
    soroush
    wrote on 24 Feb 2011, 19:29 last edited by
    #1

    Hi there

    I'm writing a program that it's ui contents will change frequently during runtime. I would like to save their state on exit and load last season on startup.

    For example I have a QToolbar and a QComboBox on it. At runtime, some items may be added to combo box and some may be removed. Program needs to save current state of combo in a file and load it again next run. Currently it just simply saves items in a text file when exiting, and in the next run reads items again and inserts them into combo. I just wondered, is there a more advanced way to save a widget in Qt's XML format like an automatically-created .ui file? I would like to save and load state in ui files. or maybe something like restoreState of QMainWindow...

    What you suggest?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on 24 Feb 2011, 20:04 last edited by
      #2

      Nope, you'll have to do it yourself. Widget states can not be serialized in a sensible way.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vishwajeet
        wrote on 25 Feb 2011, 04:45 last edited by
        #3

        You have to do it your self, one way could be use Qt's state machine framework

        Born To Code !!!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on 25 Feb 2011, 06:27 last edited by
          #4

          @vishwajeet:
          How would you employ Qt State machine framework for saving the state of widgets?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dialingo
            wrote on 25 Feb 2011, 09:03 last edited by
            #5

            You need to find out what properties excactly you want to restore and then make these proerties persistant.
            There are several persistence technologies in Qt. Choose the one which serves your needs best:

            QSettings is frequently used for storing window settings .
            XML, SQLITE and QDataStream -> QFile can be used as well but are more heavy weight.

            At runtime no such thing as an .ui file is used. You need to walk through your widget tree and extract the information you want to keep for the next session. For hierarchical data XML is always a good choice. Qt makes using XML very easy by the way.

            1 Reply Last reply
            0
            • V Offline
              V Offline
              vishwajeet
              wrote on 25 Feb 2011, 10:04 last edited by
              #6

              Andre it would not be that straight forward. In particular dynamic UI generation

              Make use of state machine framework, Qt property system and any storage setting mentioned by dialingo

              Lets say we derive from QAbstractState to CWidgetState::CWidgetState(QWidget *parent)

              Additional API to persist state of widget on disk as part of CWidgetState class

              // Loads the setting data from file, and using QMetaProperty, assign it to appropriate widget.
              @CWidgetState::loadState(const QString &fileName)@

              // Save QMetaProperty of the widget (including child widgets) into file,
              @CWidgetState::saveState(const QString &fileName)@

              Just to load and save property once, Qt state machine might not be required but for runtime transitions like undo/redo, this mechanism would be useful.

              Born To Code !!!

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 25 Feb 2011, 10:41 last edited by
                #7

                [quote author="vishwajeet" date="1298628290"]Andre it would not be that straight forward. In particular dynamic UI generation

                Make use of state machine framework, Qt property system and any storage setting mentioned by dialingo

                Lets say we derive from QAbstractState to CWidgetState::CWidgetState(QWidget *parent)

                Additional API to persist state of widget on disk as part of CWidgetState class

                // Loads the setting data from file, and using QMetaProperty, assign it to appropriate widget.
                @CWidgetState::loadState(const QString &fileName)@

                // Save QMetaProperty of the widget (including child widgets) into file,
                @CWidgetState::saveState(const QString &fileName)@

                Just to load and save property once, Qt state machine might not be required but for runtime transitions like undo/redo, this mechanism would be useful.
                [/quote]
                OK, so as I thought, the state machine API as such does not really help you at all with the persistence. I just wanted to know if I missed something there.

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  m.kraker
                  wrote on 26 Feb 2011, 07:37 last edited by
                  #8

                  Have a look at
                  http://www.qtforum.org/article/35360/easy-to-apply-persistence-class-at-google-code.html

                  1 Reply Last reply
                  0

                  1/8

                  24 Feb 2011, 19:29

                  • Login

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