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?
QtWS25 Last Chance

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 thippu

    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.
    VRoninV Offline
    VRoninV Offline
    VRonin
    wrote on 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

    jsulmJ J.HilkJ 2 Replies Last reply
    3
    • VRoninV VRonin

      @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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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
      0
      • jsulmJ jsulm

        @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 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.

        jsulmJ VRoninV 3 Replies Last reply
        0
        • T thippu

          @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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

            @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.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on 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
            • jsulmJ jsulm

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

              T Offline
              T Offline
              thippu
              wrote on last edited by
              #10

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

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

              Really, you are the author?. :-)

              jsulmJ 1 Reply Last reply
              0
              • T thippu

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

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

                Really, you are the author?. :-)

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 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
                1
                • VRoninV VRonin

                  @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.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on 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
                  • jsulmJ jsulm

                    @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 last edited by
                    #13

                    @jsulm oh, yeah true I did edit once.

                    1 Reply Last reply
                    0
                    • T thippu

                      @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.

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on 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
                      4
                      • VRoninV VRonin

                        @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 last edited by thippu
                        #15

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

                        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