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. Why is it wrong to write this in a header file?
Forum Updated to NodeBB v4.3 + New Features

Why is it wrong to write this in a header file?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 7 Posters 1.3k Views 4 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.
  • C Offline
    C Offline
    canid
    wrote on last edited by
    #1

    hello,Why is it wrong to write this in a header file?

    QSettings config("HKEY_LOCAL_MACHINE\\SOFTWARE\\mysoft", QSettings::NativeFormat);
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The biggest two reasons:

      • It violates one definition rule i.e. if you include this in multiple files you get multiple instances of the same object and it won't link.

      • QSettings inherits QObject, which means it can't outlive QApplication object, which is usually created in main() scope. In short - don't create global or static QObjects.

      1 Reply Last reply
      7
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Just as a note.
        You can have in .h if inside a class.

        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
            QSettings config{"HKEY_LOCAL_MACHINE\\SOFTWARE\\mysoft", QSettings::NativeFormat};
        .......
        

        note the { }

        1 Reply Last reply
        1
        • C Offline
          C Offline
          canid
          wrote on last edited by canid
          #4

          @Chris-Kawa @mrjj
          thank you
          It would be nice if QSettings had a function that defined the construct parameters in the CPP file so that no memory was allocated repeatedly

          mrjjM JonBJ 2 Replies Last reply
          0
          • C canid

            @Chris-Kawa @mrjj
            thank you
            It would be nice if QSettings had a function that defined the construct parameters in the CPP file so that no memory was allocated repeatedly

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @canid
            Hi
            You mean the "Set" version of
            https://doc.qt.io/qt-5/qsettings.html#fileName

            ?

            C 1 Reply Last reply
            0
            • mrjjM mrjj

              @canid
              Hi
              You mean the "Set" version of
              https://doc.qt.io/qt-5/qsettings.html#fileName

              ?

              C Offline
              C Offline
              canid
              wrote on last edited by
              #6

              @mrjj
              Yes, I think it should have this function: QSettings::setParameters(const QString & Filename, QSettings::Format Format)

              mrjjM 1 Reply Last reply
              0
              • C canid

                @mrjj
                Yes, I think it should have this function: QSettings::setParameters(const QString & Filename, QSettings::Format Format)

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @canid
                Ok. I understand.
                Not sure there is a workaround. seems not.

                I used it mostly via a class so didn't notice but I can relate to your use case.

                That said. They made QSettings cheap to create so it might not be a huge loss.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It doesn't seem to be intended to be used like that, so you're kinda bending a square to look like a circle and complaining it still has sharp corners after that.
                  Keep in mind that QSettings does periodic syncing, so keeping it open for long time (entire app lifetime in your case) is not a good idea if you don't use it often. Unless you're working with some huge data set it's easier to just create an instance when you actually need it and keep it only for the short time you use it.

                  1 Reply Last reply
                  3
                  • C canid

                    @Chris-Kawa @mrjj
                    thank you
                    It would be nice if QSettings had a function that defined the construct parameters in the CPP file so that no memory was allocated repeatedly

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

                    @canid said in Why is it wrong to write this in a header file?:

                    It would be nice if QSettings had a function that defined the construct parameters in the CPP file so that no memory was allocated repeatedly

                    The rest of you seem to understand, I don't, what memory is being allocated repeatedly?

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #10

                      @canid said in Why is it wrong to write this in a header file?:

                      QSettings config("HKEY_LOCAL_MACHINE\SOFTWARE\mysoft", QSettings::NativeFormat);

                      I would understand that you don't want to type in this long line every time you are using QSettings. However, most of the time there is no reason to change the default places to store your settings. Nevertheless, you need to configure it properly to use storage for your own application only. The way you do that is that you set organization name and application name in QCoreApplication. Everytime you call the constructor of QSettings it will use these to configure the path to your storage.

                      It would be nice if QSettings had a function that defined the construct parameters in the CPP file so that no memory was allocated repeatedly

                      I have to agree with @JonB on this that I don't understand your concerns. Memory allocation is somehow expensive when allocating on the heap. There is no valid reason to do that for QSettings. Allocation on the stack, however, does not take any time at all. You loose marginally little time to initialize QSettings. But actually setting or reading a value using QSettings is an I/O operation which takes a lot longer than that. One major point here: Don't prematurely optimize!

                      My advice is this:

                      void foo()
                      {
                          QSettings config; // use QCoreApplication::setOrganizationName/setApplicationName to configure in main()
                          ...
                      }
                      

                      This will be plenty fast.

                      J.HilkJ 1 Reply Last reply
                      4
                      • S SimonSchroeder

                        @canid said in Why is it wrong to write this in a header file?:

                        QSettings config("HKEY_LOCAL_MACHINE\SOFTWARE\mysoft", QSettings::NativeFormat);

                        I would understand that you don't want to type in this long line every time you are using QSettings. However, most of the time there is no reason to change the default places to store your settings. Nevertheless, you need to configure it properly to use storage for your own application only. The way you do that is that you set organization name and application name in QCoreApplication. Everytime you call the constructor of QSettings it will use these to configure the path to your storage.

                        It would be nice if QSettings had a function that defined the construct parameters in the CPP file so that no memory was allocated repeatedly

                        I have to agree with @JonB on this that I don't understand your concerns. Memory allocation is somehow expensive when allocating on the heap. There is no valid reason to do that for QSettings. Allocation on the stack, however, does not take any time at all. You loose marginally little time to initialize QSettings. But actually setting or reading a value using QSettings is an I/O operation which takes a lot longer than that. One major point here: Don't prematurely optimize!

                        My advice is this:

                        void foo()
                        {
                            QSettings config; // use QCoreApplication::setOrganizationName/setApplicationName to configure in main()
                            ...
                        }
                        

                        This will be plenty fast.

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @SimonSchroeder said in Why is it wrong to write this in a header file?:

                        However, most of the time there is no reason to change the default places to store your settings

                        That's just presumptuous. I personally never use the default settings.

                        However, most of the time there is no reason to change the default places to store your settings

                        For once, it prevents proper polymorphism.

                        The way you do that is that you set organization name and application name in QCoreApplication. Everytime you call the constructor of QSettings it will use these to configure the path to your storage.

                        doesn't help you if you want any format but the NativeFormat, I use IniFormat to have consistency across all platforms.

                        I agree with @canid one should be able to set the filename and format somewhere else but the constructor

                        @Chris-Kawa said

                        QSettings does periodic syncing

                        yes, which is the only reason its derived from QObject, to hook into the QEvent System to flush on repaint requests

                        In my opinion, this has more drawbacks then benefits. Especially as the user is encouraged to create and destroy QSettings objects on the fly and therefore they never receive the update request


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        SGaistS 1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @SimonSchroeder said in Why is it wrong to write this in a header file?:

                          However, most of the time there is no reason to change the default places to store your settings

                          That's just presumptuous. I personally never use the default settings.

                          However, most of the time there is no reason to change the default places to store your settings

                          For once, it prevents proper polymorphism.

                          The way you do that is that you set organization name and application name in QCoreApplication. Everytime you call the constructor of QSettings it will use these to configure the path to your storage.

                          doesn't help you if you want any format but the NativeFormat, I use IniFormat to have consistency across all platforms.

                          I agree with @canid one should be able to set the filename and format somewhere else but the constructor

                          @Chris-Kawa said

                          QSettings does periodic syncing

                          yes, which is the only reason its derived from QObject, to hook into the QEvent System to flush on repaint requests

                          In my opinion, this has more drawbacks then benefits. Especially as the user is encouraged to create and destroy QSettings objects on the fly and therefore they never receive the update request

                          SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @J-Hilk isn't QSettings::setPath what you are looking for to change the path outside the constructor ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          J.HilkJ 1 Reply Last reply
                          2
                          • SGaistS SGaist

                            @J-Hilk isn't QSettings::setPath what you are looking for to change the path outside the constructor ?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @SGaist it helps sure, but

                            Warning: This function doesn't affect existing QSettings objects.

                            it only effects Qsettings instances, that are not yet created, so not really 😔


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            SGaistS 1 Reply Last reply
                            0
                            • J.HilkJ J.Hilk

                              @SGaist it helps sure, but

                              Warning: This function doesn't affect existing QSettings objects.

                              it only effects Qsettings instances, that are not yet created, so not really 😔

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @J-Hilk indeed, I was thinking about it at initialisation stage of your application.

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J.HilkJ 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @J-Hilk indeed, I was thinking about it at initialisation stage of your application.

                                J.HilkJ Offline
                                J.HilkJ Offline
                                J.Hilk
                                Moderators
                                wrote on last edited by
                                #15

                                @SGaist mmh 🤔 right,

                                May be a better approach, than what I currently have. I'll have to test it. Thanks!


                                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                Q: What's that?
                                A: It's blue light.
                                Q: What does it do?
                                A: It turns blue.

                                1 Reply Last reply
                                0

                                • Login

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