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. Can you explain me this code?
Forum Updated to NodeBB v4.3 + New Features

Can you explain me this code?

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 1.8k 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.
  • T Offline
    T Offline
    thippu
    wrote on 16 Oct 2018, 07:54 last edited by thippu
    #1

    1)I have code like this

    void MainWindow::openInifile()
    {
         QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));
         setSettingsObject(settings);//I think it is passing its content that is a reference, am I right?
    }
    
    1. In the function, they are creating the QSettings object on the stack memory but passing the heap memory of the same type reference. will that stack object get deleted when it goes out of scope? and let me know the reason and what is the advantage of it.
    J V 2 Replies Last reply 16 Oct 2018, 07:57
    0
    • T thippu
      16 Oct 2018, 07:54

      1)I have code like this

      void MainWindow::openInifile()
      {
           QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));
           setSettingsObject(settings);//I think it is passing its content that is a reference, am I right?
      }
      
      1. In the function, they are creating the QSettings object on the stack memory but passing the heap memory of the same type reference. will that stack object get deleted when it goes out of scope? and let me know the reason and what is the advantage of it.
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 16 Oct 2018, 07:57 last edited by jsulm
      #2

      @thippu said in Can you explain me this code?:

      setSettingsObject

      Can you show its signature?

      "I think it is passing its content that is a reference, am I right?" - can't you simply check its declaration to answer this question?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      T 1 Reply Last reply 16 Oct 2018, 08:01
      1
      • J jsulm
        16 Oct 2018, 07:57

        @thippu said in Can you explain me this code?:

        setSettingsObject

        Can you show its signature?

        "I think it is passing its content that is a reference, am I right?" - can't you simply check its declaration to answer this question?

        T Offline
        T Offline
        thippu
        wrote on 16 Oct 2018, 08:01 last edited by
        #3

        @jsulm said in Can you explain me this code?:

        Can you show its signature?

        void setSettingsObject(const QSettings &settings);

        J 1 Reply Last reply 16 Oct 2018, 08:02
        0
        • T thippu
          16 Oct 2018, 08:01

          @jsulm said in Can you explain me this code?:

          Can you show its signature?

          void setSettingsObject(const QSettings &settings);

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 16 Oct 2018, 08:02 last edited by jsulm
          #4

          @thippu In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
          Actually, as long as setSettingsObject does not store a pointer to settings there will not be any error. Most probably they just assign it to an instance variable, in which case it is copied (assignment/copy operator).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          T 1 Reply Last reply 16 Oct 2018, 08:11
          1
          • T thippu
            16 Oct 2018, 07:54

            1)I have code like this

            void MainWindow::openInifile()
            {
                 QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));
                 setSettingsObject(settings);//I think it is passing its content that is a reference, am I right?
            }
            
            1. In the function, they are creating the QSettings object on the stack memory but passing the heap memory of the same type reference. will that stack object get deleted when it goes out of scope? and let me know the reason and what is the advantage of it.
            V Offline
            V Offline
            VRonin
            wrote on 16 Oct 2018, 08:04 last edited by
            #5

            @thippu said in Can you explain me this code?:

            QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));

            yeah, this is really poor. Since QSettings is a QObject it compiles as it uses this constructor but the object allocated in the heap is unused and leaked.
            Whoever wrote that code should undergo dire punishment

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            J J 2 Replies Last reply 16 Oct 2018, 08:07
            3
            • V VRonin
              16 Oct 2018, 08:04

              @thippu said in Can you explain me this code?:

              QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));

              yeah, this is really poor. Since QSettings is a QObject it compiles as it uses this constructor but the object allocated in the heap is unused and leaked.
              Whoever wrote that code should undergo dire punishment

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 16 Oct 2018, 08:07 last edited by
              #6

              @VRonin Oh, somehow I missed that "new" :-)

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              T 1 Reply Last reply 16 Oct 2018, 08:13
              0
              • J jsulm
                16 Oct 2018, 08:02

                @thippu In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?
                Actually, as long as setSettingsObject does not store a pointer to settings there will not be any error. Most probably they just assign it to an instance variable, in which case it is copied (assignment/copy operator).

                T Offline
                T Offline
                thippu
                wrote on 16 Oct 2018, 08:11 last edited by
                #7

                @jsulm said in Can you explain me this code?:

                In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?

                It was from the example "Settings editor", I'm studying the mechanism of it.

                J V 3 Replies Last reply 16 Oct 2018, 08:12
                0
                • T thippu
                  16 Oct 2018, 08:11

                  @jsulm said in Can you explain me this code?:

                  In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?

                  It was from the example "Settings editor", I'm studying the mechanism of it.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 16 Oct 2018, 08:12 last edited by
                  #8

                  @thippu See what @VRonin wrote. I didn't notice the "new QSettings", need to read more carefully :-)

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • T thippu
                    16 Oct 2018, 08:11

                    @jsulm said in Can you explain me this code?:

                    In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?

                    It was from the example "Settings editor", I'm studying the mechanism of it.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 16 Oct 2018, 08:12 last edited by
                    #9

                    @thippu said in Can you explain me this code?:

                    example "Settings editor"

                    Oh, if this is from official Qt example than you should file a bug as this is a bad example then.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • J jsulm
                      16 Oct 2018, 08:07

                      @VRonin Oh, somehow I missed that "new" :-)

                      T Offline
                      T Offline
                      thippu
                      wrote on 16 Oct 2018, 08:13 last edited by
                      #10

                      @jsulm said in Can you explain me this code?:

                      Oh, somehow I missed that "new" :-)

                      Really, you are the author?. :-)

                      J 1 Reply Last reply 16 Oct 2018, 08:14
                      0
                      • T thippu
                        16 Oct 2018, 08:13

                        @jsulm said in Can you explain me this code?:

                        Oh, somehow I missed that "new" :-)

                        Really, you are the author?. :-)

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 16 Oct 2018, 08:14 last edited by
                        #11

                        @thippu No, I'm not else @VRonin would punish me :-)
                        I mean I did not notice it then I read your first post in this thread. I think I didn't notice it because I read it while you were stil changing it. It was first

                        QSettings settings;
                        

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        T 1 Reply Last reply 16 Oct 2018, 08:16
                        1
                        • V VRonin
                          16 Oct 2018, 08:04

                          @thippu said in Can you explain me this code?:

                          QSettings settings(new QSettings("filename path and filename",QSettings::IniFormat));

                          yeah, this is really poor. Since QSettings is a QObject it compiles as it uses this constructor but the object allocated in the heap is unused and leaked.
                          Whoever wrote that code should undergo dire punishment

                          J Offline
                          J Offline
                          J.Hilk
                          Moderators
                          wrote on 16 Oct 2018, 08:15 last edited by
                          #12

                          @VRonin said in Can you explain me this code?:

                          Whoever wrote that code should undergo dire punishment

                          Well, should be possible. It's been written here

                          http://doc.qt.io/qt-5/qtwidgets-tools-settingseditor-mainwindow-cpp.html

                          should be in the docu change log right :)


                          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
                          1
                          • J jsulm
                            16 Oct 2018, 08:14

                            @thippu No, I'm not else @VRonin would punish me :-)
                            I mean I did not notice it then I read your first post in this thread. I think I didn't notice it because I read it while you were stil changing it. It was first

                            QSettings settings;
                            
                            T Offline
                            T Offline
                            thippu
                            wrote on 16 Oct 2018, 08:16 last edited by
                            #13

                            @jsulm oh, yeah true I did edit once.

                            1 Reply Last reply
                            0
                            • T thippu
                              16 Oct 2018, 08:11

                              @jsulm said in Can you explain me this code?:

                              In this case you need to check what setSettingsObject does to know whether it is a bug or not. Can you show its content?

                              It was from the example "Settings editor", I'm studying the mechanism of it.

                              V Offline
                              V Offline
                              VRonin
                              wrote on 16 Oct 2018, 09:17 last edited by VRonin
                              #14

                              @thippu said in Can you explain me this code?:

                              It was from the example "Settings editor", I'm studying the mechanism of it.

                              The example is correct: typedef QSharedPointer<QSettings> SettingsPtr; and then

                              SettingsPtr settings(new QSettings(locationDialog->format(),
                                                                     locationDialog->scope(),
                                                                     locationDialog->organization(),
                                                                     locationDialog->application()));
                              

                              or SettingsPtr settings(new QSettings(fileName, QSettings::IniFormat));

                              The problem is you took the liberty of replacing a shared pointer with a stack object

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              T 1 Reply Last reply 16 Oct 2018, 10:13
                              4
                              • V VRonin
                                16 Oct 2018, 09:17

                                @thippu said in Can you explain me this code?:

                                It was from the example "Settings editor", I'm studying the mechanism of it.

                                The example is correct: typedef QSharedPointer<QSettings> SettingsPtr; and then

                                SettingsPtr settings(new QSettings(locationDialog->format(),
                                                                       locationDialog->scope(),
                                                                       locationDialog->organization(),
                                                                       locationDialog->application()));
                                

                                or SettingsPtr settings(new QSettings(fileName, QSettings::IniFormat));

                                The problem is you took the liberty of replacing a shared pointer with a stack object

                                T Offline
                                T Offline
                                thippu
                                wrote on 16 Oct 2018, 10:13 last edited by thippu
                                #15

                                @VRonin you are right, sorry, I thought that time It would not matter.

                                1 Reply Last reply
                                0

                                6/15

                                16 Oct 2018, 08:07

                                • Login

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