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 store object in QSettings?

How to store object in QSettings?

Scheduled Pinned Locked Moved Solved General and Desktop
qsettings
11 Posts 3 Posters 4.1k 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 SiddharthRC
    2 Nov 2015, 12:52

    I have an desktop application(Qt 5.5) with multiple dialogs,
    in the login dialog there is an object initialised to which I need access when the user is logging out of the application, how do I store this object so that I can get a handle to it whenever my user wants to logout.

    T Offline
    T Offline
    TheBadger
    wrote on 2 Nov 2015, 13:12 last edited by
    #2

    @SiddharthRC It sounds like QSettings is not the solution that you are after. QSettings is used for storing information between different run times of an application, thus closing the app and opening it again.

    And, storing an object in QSettings is less of a good idea, perhaps the properties of an object.

    I suggest rather look at

    1. Dependency injection: Pass the object to the dialog/class that will need it.
    2. Some global container: Look at the singleton design pattern.
    3. Perhaps a database: SQLite database perhaps.

    What type of information does the object contain for which you need the handle when the user wants to log out?


    Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SiddharthRC
      wrote on 2 Nov 2015, 13:35 last edited by
      #3

      the thing is when the application goes from one dialog to another..the desktop is seen for a brief moment of time...I am applying a background dialog which will be present at all times after user logs in...but it should go once the user logs out..so I would need a handle to it when the user is logging out..and he can log out from any dialog..so thats why I need something that can hold a reference to the background dialog and can be accessed from anywhere..just like QSettings

      R T 2 Replies Last reply 2 Nov 2015, 13:41
      0
      • S SiddharthRC
        2 Nov 2015, 13:35

        the thing is when the application goes from one dialog to another..the desktop is seen for a brief moment of time...I am applying a background dialog which will be present at all times after user logs in...but it should go once the user logs out..so I would need a handle to it when the user is logging out..and he can log out from any dialog..so thats why I need something that can hold a reference to the background dialog and can be accessed from anywhere..just like QSettings

        R Offline
        R Offline
        raf924
        wrote on 2 Nov 2015, 13:41 last edited by
        #4

        @SiddharthRC I agree with @TheBadger you should use the singleton pattern.

        For example:

        public class MyObject{
        private static MyObject * object = null;
        public static MyObject * load(){ 
        if( MyObject::object == null){
         MyObject::object = new MyObject();
        }
        return MyObject::object;
         }
        private MyObject();
        }
        
        1 Reply Last reply
        0
        • S SiddharthRC
          2 Nov 2015, 13:35

          the thing is when the application goes from one dialog to another..the desktop is seen for a brief moment of time...I am applying a background dialog which will be present at all times after user logs in...but it should go once the user logs out..so I would need a handle to it when the user is logging out..and he can log out from any dialog..so thats why I need something that can hold a reference to the background dialog and can be accessed from anywhere..just like QSettings

          T Offline
          T Offline
          TheBadger
          wrote on 2 Nov 2015, 13:42 last edited by TheBadger 11 Feb 2015, 13:48
          #5

          @SiddharthRC

          To just iterate the QSettings docs:

          Users normally expect an application to remember its settings across sessions.

          Across sessions mean closing the app and opening the app. QSettings "might" work, but is not the correct solution.

          so thats why I need something that can hold a reference to the background dialog and can be accessed from anywhere

          From your explanation I really suggest the Singleton design pattern
          I suggest nr 2 for simplicity, never ever use number 3 :)

          So say you have a singleton (not all functions shown, refer to the link):

          Class MyDialog (
          public: 
              static MyDialog& instance() { ... }
          };
          

          You can access it from anywhere as follow:

          #include "MyDialog.h"
          
          void OtherDialog::myFunction() {
              MyDialog::instance().doSomething(); 
          }
          

          [Edit: added code example]


          Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

          S 1 Reply Last reply 3 Nov 2015, 15:25
          0
          • S Offline
            S Offline
            SiddharthRC
            wrote on 2 Nov 2015, 13:45 last edited by
            #6

            I think Singleton..should work..let me try..thanks a ton :)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SiddharthRC
              wrote on 2 Nov 2015, 14:28 last edited by
              #7
              This post is deleted!
              R 1 Reply Last reply 2 Nov 2015, 14:37
              0
              • S SiddharthRC
                2 Nov 2015, 14:28

                This post is deleted!

                R Offline
                R Offline
                raf924
                wrote on 2 Nov 2015, 14:37 last edited by
                #8

                @SiddharthRC What do you mean? Also this seems to be a different issue so maybe create another topic. And please stop using dots like that.

                1 Reply Last reply
                0
                • T TheBadger
                  2 Nov 2015, 13:42

                  @SiddharthRC

                  To just iterate the QSettings docs:

                  Users normally expect an application to remember its settings across sessions.

                  Across sessions mean closing the app and opening the app. QSettings "might" work, but is not the correct solution.

                  so thats why I need something that can hold a reference to the background dialog and can be accessed from anywhere

                  From your explanation I really suggest the Singleton design pattern
                  I suggest nr 2 for simplicity, never ever use number 3 :)

                  So say you have a singleton (not all functions shown, refer to the link):

                  Class MyDialog (
                  public: 
                      static MyDialog& instance() { ... }
                  };
                  

                  You can access it from anywhere as follow:

                  #include "MyDialog.h"
                  
                  void OtherDialog::myFunction() {
                      MyDialog::instance().doSomething(); 
                  }
                  

                  [Edit: added code example]

                  S Offline
                  S Offline
                  SiddharthRC
                  wrote on 3 Nov 2015, 15:25 last edited by
                  #9

                  @TheBadger well, I tried a lot to use the Singleton pattern and I was successful but the exit of the application was not clean,Qt IDE kept saying the "Application crashed" but it behaved exactly the way it would, since it said it was crashing I had no other option other than QApplication::exit command which closes all active Qt application windows..which will do for now and does a clean exit as well.

                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    TheBadger
                    wrote on 4 Nov 2015, 06:29 last edited by TheBadger 11 Apr 2015, 06:32
                    #10

                    @SiddharthRC Yes, so that is the 'issue' with a normal singleton. When the applications closes, you sometimes need to do some special cleanup or else you can get a crash at shutdown.

                    What I normally do is create a singleton using a static instance member (Nr 1 in the link provided), then after the call to QApplication::exec() I delete the singleton. As follow:

                    void main(...)
                    {
                        QApplication a;
                        //... <- Application setup
                        MyDialog::instance(); // <- Force the creation during setup
                        int result = a.exec(); // <- Will block until the application exits
                        delete MyDialog::instance(); // <- Clean up the singleton to close it properly
                        return result;
                    }
                    

                    Some people will disagree with this method, but guidelines, a coding standard and good documentation goes a long way. You can also connect the QApplication::aboutToQuit() signal to the QObject::deleteLater() slot on the singleton object (this is my approach lately).

                    Also have a look at Q_GLOBAL_STATIC, it solves many of the issues around initialisation of a singleton in a threaded application (I think the biggest point of debate on the singleton).

                    [Edit: small update to code]


                    Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

                    S 1 Reply Last reply 4 Nov 2015, 06:44
                    1
                    • T TheBadger
                      4 Nov 2015, 06:29

                      @SiddharthRC Yes, so that is the 'issue' with a normal singleton. When the applications closes, you sometimes need to do some special cleanup or else you can get a crash at shutdown.

                      What I normally do is create a singleton using a static instance member (Nr 1 in the link provided), then after the call to QApplication::exec() I delete the singleton. As follow:

                      void main(...)
                      {
                          QApplication a;
                          //... <- Application setup
                          MyDialog::instance(); // <- Force the creation during setup
                          int result = a.exec(); // <- Will block until the application exits
                          delete MyDialog::instance(); // <- Clean up the singleton to close it properly
                          return result;
                      }
                      

                      Some people will disagree with this method, but guidelines, a coding standard and good documentation goes a long way. You can also connect the QApplication::aboutToQuit() signal to the QObject::deleteLater() slot on the singleton object (this is my approach lately).

                      Also have a look at Q_GLOBAL_STATIC, it solves many of the issues around initialisation of a singleton in a threaded application (I think the biggest point of debate on the singleton).

                      [Edit: small update to code]

                      S Offline
                      S Offline
                      SiddharthRC
                      wrote on 4 Nov 2015, 06:44 last edited by
                      #11

                      @TheBadger thanks a lot badger you have been of great help..

                      1 Reply Last reply
                      0

                      11/11

                      4 Nov 2015, 06:44

                      • Login

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