PDA

View Full Version : Delete and re-create Qt Designer generated widgets



quimnuss
20th August 2015, 12:42
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?

anda_skoa
20th August 2015, 13:32
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,
_

quimnuss
20th August 2015, 16:14
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.

anda_skoa
20th August 2015, 17:10
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,
_

quimnuss
24th August 2015, 12:52
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.

anda_skoa
25th August 2015, 08:53
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,
_

quimnuss
25th August 2015, 14:23
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.

anda_skoa
25th August 2015, 22:12
Is there a graphical way to copy the contents of the main window ui ?

Select, copy -> paste



If not, is it ok if I copy the rellevant part of the xml code of the ui form?


Yes

Cheers,
_