Qt World Summit: Register Today!

Multi-page application strategy for QStackedWidget

  • Hello everyone,
    I'm new to the forums and to Qt in general, but not to C/C++ programming.
    Before coming here I've been documenting myself for a while and what I hope some more experienced Qt developer could help me.

    I am developing a multi-paged application under a Qt 4.8 framework and I'm facing a "problem" I'd like to solve.

    The strategy I'm using is a unique QMainWindow with a QStackedWidget in it and some other widgets.

    I created a Controller class to which every QWidget (e.g. pages) register the UI-flow relevant widgets by the means of SIGNALS/SLOT assignment.

    When a UI-relevant widget gets clicked, the Controller has a slot triggered to which is passed the name of the next page (QWidget class) via a string.

    A dynamic instantiator creates then a new instance of the QWidget subclass resolving the string into a type and calls a function (passing the instance of the QWidget subclass) in the QMainWindow that removes the current widget from the QStackedWidget and adds the new one making it visible.

    Would you use this strategy or suggest/know something better?

    *I noticed that the QStackedWidget.count at startup, after just one call of .addWidget is executed, is ==3. Is that supposed to behave like that?

    Thank you very much.

  • I would use the QStackedLayout and add all widgets and show them by using QStackedLayout::widget(int index). This is mentioned in the docs: http://qt-project.org/doc/qt-4.8/qstackedlayout.html

  • Hello, thanks for your answer.
    My concern was that by instantiating all the Widgets at once, even if unused, i could use a lot of memory.
    Isn't any better solution available?

  • BTW...i'm using the approach you described in an application which is extended by plugins to avoid loading of all plugins at start.

  • Moderators

    [quote author="hontonio" date="1418935805"]My concern was that by instantiating all the Widgets at once, even if unused, i could use a lot of memory.
    Isn't any better solution available?[/quote]

    How much memory will all the widgets take?

    How much memory do you have available?

    How much time does it take to instantiate all of the widgets together?

    If #1 is much smaller than #2, and #3 is small, then instantiating them all at the beginning is the best approach.

    If the above conditions don't hold, then dynamic instantiation is better. However, since you only have 1 widget "alive" at a time, there's no need for a QStackedWidget.

  • Hi!

    I created a plugin interface. At frist the plugin itself is instantiated and it it shows an icon on a toolbar. If the icon was clicked the widget will be created. The pluginwidget lives as long as you click "close plugin" or quit the application. It is possible to open more than one plugin-instances with widgets. The user can close every widget by choosing the plugin and clicking close.

    You could also add warnings if more than 3 plugins are open or the ram uses too much memory (1/3 of the whole ram or by user-setting).

Log in to reply