The page is displayed and quickly closed after clicking on a button



  • hey, i have some troubles displaying a page after clicking on a button.
    this is my code
    @ajoutport p;
    p.show();
    @

    when i click in the button the new page is displayed and so quickly closed !! what is the problem? thank's



  • The call to slot show immediately returns. If you do this in a slot, e.g. like

    @
    void myClass:fancySlot()
    {
    ajoutport p;
    p.show();
    }
    @

    your widget in variable p will be destructed immediately after your call to show.



  • could you please explain it more? I didn't understand you! :)





  • i resolve the problem, as i'm a beginner in Qt and c++ i commit a simple error in c++.

    must only use new to declare and initialize ajoutport. thank you



  • Let’s have yet one useless response...

    http://doc.qt.nokia.com/latest/qwidget.html#show
    http://doc.qt.nokia.com/latest/qdialog.html#exec

    Function names are really consistent across Qt, learn what some basic ones do and you can do it without even first studying half of the framework... Doc is the way to go, anyway.



  • [quote author="dhay06" date="1296510964"]i resolve the problem, as i'm a beginner in Qt and c++ i commit a simple error in c++.

    must only use new to declare and initialize ajoutport. thank you [/quote]

    Nice. That creates a fine memory leak.



  • @
    void myClass:fancySlot()
    {
    ajoutport* p = new ajoutport(this);
    p->show();
    }
    @

    this would not create the leak :-)



  • It stills creates garbage if one activates that slot more than once (and closes that subwindow everytime).



  • but it is deleted, when myClass is deleted, so it's no memory leak, but yes, it's not optimal...



  • To make it a bit better, you can use:

    @
    void myClass:fancySlot()
    {
    ajoutport* p = new ajoutport(this);
    p->setAttribute (Qt::WA_DeleteOnClose, true);
    p->show();
    }
    @

    then the widget is deleted, when it is closed.



  • [quote author="peppe" date="1296553193"]It stills creates garbage if one activates that slot more than once (and closes that subwindow everytime).[/quote]

    They’ll be destroyed as designed; when the object that has this specified slot is destroyed. Not before. And regardless of amount of created objects.



  • [quote]
    @
    p->setAttribute (Qt::WA_DeleteOnClose, true);
    @
    [/quote]

    Exactly.

    [quote]when the object that has this specified slot is destroyed[/quote]

    ... which was exactly my point. You don't destroy it => you're creating garbage.



  • [quote author="peppe" date="1296555543"][quote]
    @
    p->setAttribute (Qt::WA_DeleteOnClose, true);
    @
    [/quote]

    Exactly.

    [quote]when the object that has this specified slot is destroyed[/quote]

    ... which was exactly my point. You don't destroy it => you're creating garbage.[/quote]

    Often that’s desired, though.



  • [quote author="Smar" date="1296554983"]
    [quote author="peppe" date="1296553193"]It stills creates garbage if one activates that slot more than once (and closes that subwindow everytime).[/quote]

    They’ll be destroyed as designed; when the object that has this specified slot is destroyed. Not before. And regardless of amount of created objects.
    [/quote]

    With a long running application, this can chew up a reasonable/too big amount of the memory. This is an issue on mobile/embedded platforms.

    So, as this forum is a learning resource for many, this should not be left uncommented and strongly advised against. Leaving the additional aspects of proper design in the box for now.



  • [quote author="Smar" date="1296555675"]
    [quote author="peppe" date="1296555543"][quote]
    @
    p->setAttribute (Qt::WA_DeleteOnClose, true);
    @
    [/quote]

    Exactly.

    [quote]when the object that has this specified slot is destroyed[/quote]

    ... which was exactly my point. You don't destroy it => you're creating garbage.[/quote]

    Often that’s desired, though.[/quote]

    One want's to create garbage? I'd better not comment further...

    Back to topic, setting Qt::WA_DeleteOnClose on a window does not create garbage. It leads to calling "deleteLater() ":http://doc.qt.nokia.com/latest/qobject.html#deleteLater. The event loop eventually destroys the object once it runs again. Although this is not a good solution either. If you want to peek some data out of your dialog after it is closed, your event loop might have run in the meantime and the object is destroyed, leading to a nice segmentation fault. Better call deleteLater() from the managing code.



  • [quote author="Volker" date="1296556565"]

    One want's to create garbage? I'd better not comment further...
    [/quote]

    Imagine a situation where you do some intensive cleanup in destructor of a class. Then you create 100 popups of them, which automatically are getting closed down like in interval of 5 seconds.

    Would you rather make application use CPU all of the time or just at the end after nothing else needs the resources? Sure, it’s all application and usage related, but alternatives are here.

    Going to not so excessive example, often you just are creating few widgets in modular manner (and not in QApplication or whatever main window you are using), so they’ll get killed in exactly same place they’d do when you’d use manual, and something that needs maintenance, work.



  • [quote author="Smar" date="1296557025"][quote author="Volker" date="1296556565"]

    One want's to create garbage? I'd better not comment further...
    [/quote]

    Imagine a situation where you do some intensive cleanup in destructor of a class. Then you create 100 popups of them, which automatically are getting closed down like in interval of 5 seconds.
    [/quote][/quote]

    I would spank any programmer that mauls me with that amount of popups. And before doing that I would kill -9 the application, not giving any bit of that sh...iny program even the smallest chance to clean up its mess...

    [quote author="Smar" date="1296557025"]
    Would you rather make application use CPU all of the time or just at the end after nothing else needs the resources? Sure, it’s all application and usage related, but alternatives are here.

    Going to not so excessive example, often you just are creating few widgets in modular manner (and not in QApplication or whatever main window you are using), so they’ll get killed in exactly same place they’d do when you’d use manual, and something that needs maintenance, work. [/quote]

    In that case, I create a single instance of that dialog and reuse it.



  • [quote author="Volker" date="1296558150"]
    [quote author="Smar" date="1296557025"]
    Would you rather make application use CPU all of the time or just at the end after nothing else needs the resources? Sure, it’s all application and usage related, but alternatives are here.

    Going to not so excessive example, often you just are creating few widgets in modular manner (and not in QApplication or whatever main window you are using), so they’ll get killed in exactly same place they’d do when you’d use manual, and something that needs maintenance, work. [/quote]

    In that case, I create a single instance of that dialog and reuse it.[/quote]

    Be it single or not, it’s still easier to leave the deletion to system.



  • [quote author="Smar" date="1296558384"]
    [quote author="Volker" date="1296558150"]
    In that case, I create a single instance of that dialog and reuse it.[/quote]

    Be it single or not, it’s still easier to leave the deletion to system.
    [/quote]

    That's nonsense for at least two reasons.

    Deletion by system (read: the Qt event loop) takes the very same resources (say CPU time), but you have no exact control over when it happens. And if you create a big bunch of such heavyweight (in sense of resource allocation/cleanup work) and their deletion is deferred to the end your program will eventually hang a reasonable time on shutting down - leading to our well beloved "this application does not respond" messages (eventually leading the user to abort the application)...

    If you call deleteLater() (what actually called with the window flag) the event loop will destroy the object. If it is really that time consuming, the deletion will block your program and you cannot even warn your users, eg. by setting a busy cursor.
    And sorry, if the destruction is such big work it does matter if you have to clean up once or 10 times.

    It might be easier for you as a programmer, in the sense of "I'm too lazy to think about it, so let Qt do the nasty work for me, I don't care".


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.