Results 1 to 7 of 7

Thread: delete the buttons and label permenantly

  1. #1
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default delete the buttons and label permenantly

    i have two screen,
    first screen contains 4 button and 1 label
    second screen contains 4 button and 1 label
    my app will start in first screen,,when i move to second screen first screen buttons and label should delete permenantly, so i used code
    Qt Code:
    1. void Widget:: deactivefirstscreen()
    2. {
    3. delete button1;
    4. delete button2;
    5. "
    6. "
    7. delete label1;
    8. }
    To copy to clipboard, switch view to plain text mode 
    i getting erro when command reached this method()
    Error:
    Qt Code:
    1. The program has unexpectedly finished.
    To copy to clipboard, switch view to plain text mode 

    please give me clarification for this
    Thanks in advance

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete the buttons and label permenantly

    How this method is started ?

  3. #3
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: delete the buttons and label permenantly

    when i click next screen method,this method first call deactivatefirstscreen(),and activate the secondscreenmethod()
    Quote Originally Posted by Lesiok View Post
    How this method is started ?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete the buttons and label permenantly

    What does it mean "click next screen method". Is it one from this buttons ?

  5. #5
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: delete the buttons and label permenantly

    yes lesiok
    button1()//if it clicked it moves to second screen method(),

    Qt Code:
    1. void Widget::button1()
    2. {
    3. deactivatefirstscreen(); //it should delete the labels and button in first screen
    4. activateaddscreen();//its new screen(second screen)
    5. }
    To copy to clipboard, switch view to plain text mode 
    Quote Originally Posted by Lesiok View Post
    What does it mean "click next screen method". Is it one from this buttons ?

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delete the buttons and label permenantly

    So what are you surprised? It is exactly as if the object caused its own destructor. Nothing with Qt but C++ abc.
    Change delete buttonxxx for buttonxxx.deleteLater().

  7. #7
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Thanks
    3
    Thanked 65 Times in 59 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: delete the buttons and label permenantly

    I guess you are committing "double deleting". Double deleting isn't tolerated in C++ and usually results in GP fault.

    button1, label1 and others are (I guess) child windows of firstscreen. The firstscreen gets destroyed in the end and when it gets destroyed, it destroys its children. But the children are already destroyed by you yourself explicitly. Therefore crash. Never destroy child windows explicitly. Especially, if you have built the firstscreen by a UI script (that means, if you designed the firstscreen in the Designer and activated it using setupUi()), the button1 and others are child windows of firstscreen.

    Next, if my guessing is correct, it depends whether firstscreen and addscreen are children of some "main window" or not. If they are, you cannot destroy them either (the same reason as above). hide() the firstscreen instead of deleting it. Do the same to the addscreen if you need and destroy the "main window" in the end (or let the program logic to destroy it). If firstscreen and addscreen areboth top level windows then you need to destroy firstscreen explicitly. Therefore, do not destroy button1, label1 and other windows, destroy firstscreen. You will also need to destroy addscreen later.

    The question is, whether you should destroy some windows at all. You cannot "optimize" you app this way. Your windows use only a small portion of resources used by your app so that such "optimizing" is no optimizing. If you have some "main window" then do not destroy any object explicitly. Use hide() instead of delete and note that hiding a window hides all its child windows. When you quit the app, the main window gets destroyed and this triggers an avalanche of deletions of all subordinate windows. Without a main window, you need to destroy top level windows yourself (and disable automatic deletion of windows when you quit the app because it is not clear what should be deletedand what has been already deleted).

  8. The following user says thank you to Radek for this useful post:

    iswaryasenthilkumar (26th November 2015)

Similar Threads

  1. How to delete the shadow of buttons in QT?
    By hityrj in forum Newbie
    Replies: 2
    Last Post: 30th November 2015, 01:57
  2. Replies: 6
    Last Post: 12th June 2013, 09:36
  3. Need a Label Below the toolbar buttons
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2010, 06:42
  4. Replies: 4
    Last Post: 19th February 2009, 11:10
  5. tool buttons on top of label
    By McKee in forum Qt Programming
    Replies: 5
    Last Post: 2nd November 2008, 16:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.