Use QStackedWidget as you central widget and add your widgets to this stacked widget.
Use QStackedWidget as you central widget and add your widgets to this stacked widget.
graciano (14th April 2010)
From the QMainWindow docs:In v2 the destructor of the current form gets called when you select a different form, leaving you with a dangling pointer. Thus the segfault. So in v1 you are not wasting memory because the previous form is deleted.Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
Count my vote for QStackedWidget.
graciano (14th April 2010)
You mean that ...
... allocated the memory and the ...Qt Code:
frm000 = new graus000Form;To copy to clipboard, switch view to plain text mode
... tells the QMainWindow to takes ownership of the frm000 pointer.Qt Code:
this->setCentralWidget(frm000);To copy to clipboard, switch view to plain text mode
So in this case the "appropriate time" happens everytime i set a new central widget and there is no need for any delete frm000.(?)
Beeing this true the main advantage in using a QStackedWidget will be the speed. And the main disadvantage will be that i will have all widgets loaded in memory while the programs is running ... right?
yes, no need.So in this case the "appropriate time" happens everytime i set a new central widget and there is no need for any delete frm000.(?)
Another advantage is the simplification of your code. I didn't know the answer to your memory usage question so I set up a stacked widget version of your app. Surprisingly (to me), the QStackedWidget version executable is slightly smaller and uses less memory.Beeing this true the main advantage in using a QStackedWidget will be the speed. And the main disadvantage will be that i will have all widgets loaded in memory while the programs is running ... right?
Executable size:
- Your v1 = 1,372,139 B
- Stacked Widget = 1,360,267 B
pmap results:
- Your v1 = 26,368K
- Stacked widget = 26,300K
I am attaching my .cpp .h .pro & .ui files. Give them a test drive and see if I left something out. - stackwidget.tar..bz2
graciano (15th April 2010)
hummm ... interesting thing this QStackedWidget.
But ... let me "refrase" ...
If i want to build an application based on a QMainWindow, and i don't want to have my dialogs floating on the user Desktop ... what would be the best approach?
The idea is to build independent Dialogs for each task ... and then use them in the appropriate moment by showing them in the "centralWidget".
For now we have:
1. my v1 example
2. QStackedWidget
3. ?
I think that the MDI approch does not fit in this discussion!?
[QUOTE=graciano;140033
I think that the MDI approch does not fit in this discussion!?[/QUOTE]
It depends how many widgets you can see at a time. MDI allows you to open many windows in MDI area.
You can use also use QTabWidget. And I didn't know how complicated you 4 widgets are but I think that in your case QStackedWidget is the best solution and memory usage is not such big deal in this case unless you run your app on some PC with 20MB RAM...
jaffers (16th August 2010)
Bookmarks