Results 1 to 8 of 8

Thread: Delete and re-create Qt Designer generated widgets

  1. #1
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Delete and re-create Qt Designer generated widgets

    I inherited a program that was initiated by command-line and I have to turn it into a project-based program, where you have a Open project on the menu and so on.

    I've moved everything from the QMainWindow constructor to a loadProject function.

    Now the problem arises when the user loads a project when there was already one opened. The items allocated in the heap by the mainwindow will be reasigned and therefore we have a memory leak. I can take care of the project data, but I have many graphic items allocated in the heap with a hierarchy of centralWidget->layout->scene->items and centralWidget->layout->items

    I've thought I can delete the central widget so I get rid of everything, but it's an autogenerated object located in ui_mainwindow.h. How can I reset it or rebuild it so I can assign it again once I delete it? Does anything I say make sense? Any other better approaches?
    Last edited by quimnuss; 20th August 2015 at 13:01.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    First, let me ask: why do you want to delete the widget in the first place?
    Can't you just fill the widget with new content?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    Totally legitimate question.

    The scene, for example, has several lines and points and a pixmap. As you say, one way to go would be to delete the scene with all the items in it, and also delete the other items on the other layout. And then create a new scene and other items.

    This will probably be the way I go, but I was wondering if wouldn't it be more clean and errorless to delete the entire widget since nothing in it would remain in the new project. I wonder how the other projects do this.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    It is possible and infact not very difficult to deleted and recreate the central widget.
    I was mainly asking so you could validate if that is indeed the route you want to go.

    The easiest way for delete/recreate is to take the content of the central widget out of the main window's designer form and into a separate designer form class (using the widget template).
    The ui->setupUi() of the main window will then never create the central widget's content, you can always explicitly create an instance of this new class and set it as the central widgets, delete it and create a new instance as you see fit.

    Cheers,
    _

  5. #5
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    I see, I will try that. If I understood correctly, I should create another .ui from designer which then I can assign to the main window ui form. I'll check that out.

    Could I ask more details on your view on whether that's the correct path? I am open to other possibilities.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    You create another Qt Designer form class.
    You move the content of the main window form's central widget to that.
    You leave the central widget in main window empty.
    You create an instance of the new class in the constuctor of the main window class and set it as the central widget.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2015
    Posts
    52
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    Is there a graphical way to copy the contents of the main window ui ? If not, is it ok if I copy the rellevant part of the xml code of the ui form?

    It's apparently ok to copy it through the xml code.
    Last edited by quimnuss; 25th August 2015 at 15:09.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Delete and re-create Qt Designer generated widgets

    Quote Originally Posted by quimnuss View Post
    Is there a graphical way to copy the contents of the main window ui ?
    Select, copy -> paste

    Quote Originally Posted by quimnuss View Post
    If not, is it ok if I copy the rellevant part of the xml code of the ui form?
    Yes

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 16th July 2011, 06:37
  2. Replies: 2
    Last Post: 30th March 2011, 21:20
  3. Replies: 2
    Last Post: 28th May 2010, 16:26
  4. Delete objects in Designer
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2007, 11:42
  5. How to create pop up widgets in designer.
    By gsQT4 in forum Qt Tools
    Replies: 1
    Last Post: 25th May 2006, 17:40

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.