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. The correct way of instantiating the main widget in main.cpp
QtWS25 Last Chance

The correct way of instantiating the main widget in main.cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 5 Posters 904 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by
    #1

    Hi all,

    In Qt programs we mostly have two options either to normally instantiate the main widget or dynamically using new.

    For instance:

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    
    QSuperClass* myWidget = new QSuperClass;
    myWidget->show();
    
     // or
    
    QSuperClass myWidget;
    myWidget.show();
    
    return app.exec();
    }
    

    I know the former is allocating memory dynamically (on heap) and the latter is doing the same thing manually (on stack), but when or where do we generally prefer one over another, please?

    Thanks in advance.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The first leaks QSuperClass (unless you set Qt::WA_DeleteOnClose), the second may lead to problems when your QSuperClass dtor calls something which needs the eventloop (e.g. a deleteLater()).

      My usage in real application looks similar to

      int main(int argc, char *argv[])
      {
        QApplication app(argc, argv);
        int ret = -1;
        {
          std::unique_ptr<QSuperClass> myWidget(new QSuperClass);
          myWidget->show();
          ret = app.exec();
        }
        return ret;
      }
      

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        Hi
        In main, the stack version is preferred
        as it guarantees automatic clean up.
        However, both could work and it's hard to say one is
        more correct than the other.

        1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          The first leaks QSuperClass (unless you set Qt::WA_DeleteOnClose), the second may lead to problems when your QSuperClass dtor calls something which needs the eventloop (e.g. a deleteLater()).

          My usage in real application looks similar to

          int main(int argc, char *argv[])
          {
            QApplication app(argc, argv);
            int ret = -1;
            {
              std::unique_ptr<QSuperClass> myWidget(new QSuperClass);
              myWidget->show();
              ret = app.exec();
            }
            return ret;
          }
          
          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in The correct way of instantiating the main widget in main.cpp:

          may lead to problems when your QSuperClass dtor calls something which needs the eventloop

          @Christian-Ehrlicher said in The correct way of instantiating the main widget in main.cpp:

          My usage in real application looks similar to

          How does your usage fix the problem you raised?

          I always use the stack version but if we want to be anal about this shouldn't it be like the below?

          QSuperClass* myWidget = new QSuperClass;
          myWidget->show();
          QObject::connect(&app,&QCoreApplication::aboutToQuit,myWidget,&QObject::deleteLater);
          return app.exec();
          

          "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.HilkJ 1 Reply Last reply
          4
          • VRoninV VRonin

            @Christian-Ehrlicher said in The correct way of instantiating the main widget in main.cpp:

            may lead to problems when your QSuperClass dtor calls something which needs the eventloop

            @Christian-Ehrlicher said in The correct way of instantiating the main widget in main.cpp:

            My usage in real application looks similar to

            How does your usage fix the problem you raised?

            I always use the stack version but if we want to be anal about this shouldn't it be like the below?

            QSuperClass* myWidget = new QSuperClass;
            myWidget->show();
            QObject::connect(&app,&QCoreApplication::aboutToQuit,myWidget,&QObject::deleteLater);
            return app.exec();
            
            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @VRonin

            I would say, as the unique pointer is in a local scope, it gets destroyed once app.exec returns and therefore deletes the object its pointing to.

            Or am I wrong?


            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.

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

              @VRonin

              I would say, as the unique pointer is in a local scope, it gets destroyed once app.exec returns and therefore deletes the object its pointing to.

              Or am I wrong?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @J.Hilk said in The correct way of instantiating the main widget in main.cpp:

              it gets destroyed once app.exec returns and therefore deletes the object its pointing to

              it gets destroyed once after app.exec returns and therefore deletes the object its pointing to.

              So if ~QSuperClass calls deleteLater on some object that is not a child (or grandchild, etc) of this it will be leaked as correctly pointed out bu Christian

              "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

              1 Reply Last reply
              3
              • tomyT Offline
                tomyT Offline
                tomy
                wrote on last edited by tomy
                #7

                Thank you all,
                I got some part of the subject but not all since there're sophisticated matters and need more experience to spot them appropriately.

                To sum up, we had better use the stack version unless we are too strict to pick out heap, and in this case, the version suggested by @VRonin is to be used.
                Right until here?

                So the next question is "when" will we be logically forced to use the heap version?

                PS: I became slightly glad that I found somewhere in Qt where C++ smart pointers are used advantageous! But likely they're still a de facto redundant item to Qt.

                VRoninV 1 Reply Last reply
                0
                • tomyT tomy

                  Thank you all,
                  I got some part of the subject but not all since there're sophisticated matters and need more experience to spot them appropriately.

                  To sum up, we had better use the stack version unless we are too strict to pick out heap, and in this case, the version suggested by @VRonin is to be used.
                  Right until here?

                  So the next question is "when" will we be logically forced to use the heap version?

                  PS: I became slightly glad that I found somewhere in Qt where C++ smart pointers are used advantageous! But likely they're still a de facto redundant item to Qt.

                  VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  @tomy said in The correct way of instantiating the main widget in main.cpp:

                  But likely they're still a de facto redundant item to Qt.

                  Not at all.

                  next question is "when" will we be logically forced to use the heap version?

                  If you do weird stuff in the destructor of objects created in main() then use the heap version

                  "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

                  tomyT 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    @tomy said in The correct way of instantiating the main widget in main.cpp:

                    But likely they're still a de facto redundant item to Qt.

                    Not at all.

                    next question is "when" will we be logically forced to use the heap version?

                    If you do weird stuff in the destructor of objects created in main() then use the heap version

                    tomyT Offline
                    tomyT Offline
                    tomy
                    wrote on last edited by
                    #9

                    @VRonin

                    If you do weird stuff in the destructor of objects created in main() then use the heap version

                    I will keep that in mind for further uses in the future to get more used to those "weird stuff".

                    Not at all.

                    But you wouldn't use them in the example above.

                    Will you please tell me, or better give a "simple" example, where a smart C++ pointer is used preferably, in a Qt project.

                    VRoninV 1 Reply Last reply
                    0
                    • tomyT tomy

                      @VRonin

                      If you do weird stuff in the destructor of objects created in main() then use the heap version

                      I will keep that in mind for further uses in the future to get more used to those "weird stuff".

                      Not at all.

                      But you wouldn't use them in the example above.

                      Will you please tell me, or better give a "simple" example, where a smart C++ pointer is used preferably, in a Qt project.

                      VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #10

                      @tomy said in The correct way of instantiating the main widget in main.cpp:

                      But you wouldn't use them in the example above.

                      No, because at the end of the day it's the same as stack allocation

                      Will you please tell me, or better give a "simple" example, where a smart C++ pointer is used preferably, in a Qt project.

                      Private implementations are the typical example of how to use of smart pointers in Qt. Basically if the class is not a subclass of QObject and you have to allocate on the heap you should probably think about using smart pointers

                      "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

                      1 Reply Last reply
                      3

                      • Login

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