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 delete widget if constructor fails?
QtWS25 Last Chance

How to delete widget if constructor fails?

Scheduled Pinned Locked Moved General and Desktop
7 Posts 6 Posters 2.2k 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.
  • A Offline
    A Offline
    aptyp
    wrote on last edited by
    #1

    How to prevent show widget, if it's constructor somehow failed?

    For example I use filedialog in constructor, and if cancel is clicked, I don't want to show that.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I would say it's a bad design. If your widget should not be constructed based on a condition you should check that condition before trying to build your widget not in its constructor.

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

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        i agree with SGaist.
        But if you can't do that (for whatever reason) it's better design to implement an initialization method:
        @
        MyObject* obj = new MyObject;
        if( obj->init() )
        {
        //work with "obj"
        }
        else
        {
        //error handling
        }
        @

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • P Offline
          P Offline
          PeerS
          wrote on last edited by
          #4

          Making the constructor fail as part of the possible 'normal' application behaviour is not only very bad design as already stated earlier, it is also quite dangerous. Remember, the destructor is only called on fully constructed objects. If you have an object which has to do some cleanup inside the desctructor, e.g. free some sort of resource, then if the constructor fails for whatever reason then its destructor will never be called.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aptyp
            wrote on last edited by
            #5

            I meant not exactly fails with exception or something. In my case in constructor I call filedialog, and if user haven't chosen a file we should quit.

            1 Reply Last reply
            0
            • JeroentjehomeJ Offline
              JeroentjehomeJ Offline
              Jeroentjehome
              wrote on last edited by
              #6

              Hi,
              It's always a "bad" idea to have user interface in a constructor. The event loop might not even be running when you want to display the filedialog, so strange things might happen.
              Like raven-worx showed, construct your object, when it is constructed call the "init" function of the object to select/open the file dialog. When cancel is pressed do something else.
              Greetz

              Greetz, Jeroen

              1 Reply Last reply
              0
              • M Offline
                M Offline
                MuldeR
                wrote on last edited by
                #7

                [quote author="aptyp" date="1375714650"]I meant not exactly fails with exception or something. In my case in constructor I call filedialog, and if user haven't chosen a file we should quit.[/quote]

                As others said before, the actual constructor should never fail.

                But you could use a static newInstance() function that handles everything:
                @MyWidget *MyWidget::newInstance()
                {
                MyWidget *obj = new MyWidget();
                bool success = obj->doSomethingThatMightFail();
                if(!success)
                {
                delete obj;
                obj = NULL;
                }
                return obj;
                }@

                The actual constructor of MyWidget should be made private.

                Construct a new instance like this:
                @MyWidget *widget = MyWidget::newInstance();@

                Don't forget to check whether the returned pointer is NULL ;-)

                My OpenSource software at: http://muldersoft.com/

                Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

                Go visit the coop: http://youtu.be/Jay...

                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