Results 1 to 20 of 22

Thread: How to stop multiple appearance of a widget

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to stop multiple appearance of a widget

    ....and you are still creating the dialog again and again.
    You do not want to create a new instance of the dialog every time the button is pressed.

    Qt Code:
    1. CPT::CPT(QWidget *parent):QWidget(parent)
    2. {
    3. createFind(); // create the dialog ONLY ONCE
    4. QPushButton *findbox=new QPushButton();
    5. QObject::connect(findbox,SIGNAL(clicked()),this,SLOT(find()));
    6. }
    7.  
    8. void CPT::find()
    9. {
    10. // set your defaults here
    11. fwin->show();
    12. fwin->activateWindow();
    13. fwin->raise();
    14. }
    15.  
    16. void CPT::createFind()
    17. {
    18. fwin = new QWidget;
    19. // ...
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2006
    Location
    Hyderabad
    Posts
    69
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: How to stop multiple appearance of a widget

    sir,
    Thank you very much. It is working well. Now I got the point. I was creating an instance of the window everytime I click the button. But here I got a doubt. We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage? Kindly rectify my doubt.

    Thanks once again for this useful post,
    Sarma.

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to stop multiple appearance of a widget

    Quote Originally Posted by Sarma
    sir,
    Thank you very much. It is working well. Now I got the point. I was creating an instance of the window everytime I click the button. But here I got a doubt. We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage? Kindly rectify my doubt.

    Thanks once again for this useful post,
    Sarma.
    No memory leaks caused provided you use destructor wisely to free all used memory...
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to stop multiple appearance of a widget

    Quote Originally Posted by Sarma
    We are creating the instance of the findbox in the application even before clicking it. Now for any reason if we donot click the button , doesn't that cause memory wastage?
    Since you don't supply any parent for the widget, it doesn't get destroyed automatically. So you will have to delete it in the descructor.
    A widget without a parent is always an independent window.. But why don't you just use a modeless dialog? Then you could pass your window as a parent, the dialog would show up centered to your window, and it would get automatically deleted..

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. Display multiple Images in a widget
    By jeetu_happy in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2007, 11:24

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.