views:

115

answers:

1

I'm building a Qt application that have about 30 different views (QWidgets). My idea is to use a QStackedWidget to make it easy to switch between the different views in the application. I have two different solutions of how to implement this and use as little memory as possible when the user navigates through the application.

Solution 1: Everytime I need to show a view I check if it is already in the stack. (The user might open the same view many times, maybe a view showing an item from a database). If the view is in the stack already it doesn't need to be created again and I can just show the view.

The good thing with this solution is that I reuse the views (widgets) so they only need to be created once. This is good as the UI and other stuff should look the same everytime the user show a view, so why not reuse it? The problem with this solution is that every view has childrens. Maybe an object, a QList with objects or other things. A good thing with Qt is that you can use the parent-children mechanism so that the children will be deleted when the parent is deleted. As I never delete the parent (view) I need to handle this myself as the children might need to be deleted from different times when the view is shown. (Maybe the view show a list with objects and the list should be updated from a database each time the view is shown.)

Solution 2: Everytime I need to show a QWidget I create a new one and show it. When it is not shown anymore, I delete it from memory.

This is a quite easy solution. And as I delete the views when they are not shown both the view and it's children should be deleted from memory so it shouldn't increase memory, am I right?

Which one of the solutions do you recommend?

+1  A: 

If memory is that tight, it would probably be best to just instantiate and delete as needed.

If you follow a MVC-type architecture, your data model should be distinct from your views, so the QWidget classes shouldn't keep references to the data model when not in use. That is, when your QWidget class is no longer needed, you could keep it instantiated but "nullify" references to the data model (which I presume manages it's own memory).

Simeon Fitch
Thanks! It's for the Symbian OS, so maybe memory isn't that tight, but just want to choose the best solution to work with.
Martin
I'd go with option #2 then, as it seems the simplest. If performance becomes an issue, you can revisit later (after profiling).
Simeon Fitch
I tried using both solutions. The first one is very fast but the second one worked quite fast as well and as #2 is better for memory I choosed #2. Thanks for your help!
Martin