Search code examples
c++qtqmainwindowqstackedwidget

Qt Multiple window design


I appologize for the long paragraph but its the only way I can explain this.

Basic Question: How do I have multiple windows that share some UI functionality without having it all in one giant class?

My Situation: I have a question regarding the Design of an application containing multiple windows. The windows will share several of the components on the UI (date & time, battery, etc.). Each window will also have much of its own ui functionality. My original idea was to have separate windows so that the class was not so large and the ui files were easier to look at. However, I find that I have repeated code for the shared ui functionality since I have to implement it in each class. Also, I am forced to have many connections between windows to make sure certain things are handled when changing windows.

Possible Solution: I have read that a QStackedWidget is good for multiple windows, but everything will be in one class. Unless of course there is a way of handling some of the ui in a subclass of the main ui? Without making other ui files?


Solution

  • I agree with using a QStackedWidget. I generally design my applications with the following ideologies:

    1. Each window/screen has its own class.
    2. If there is repetition of functionality or code, that part gets its own class (which is inherited by each of the window classes).
    3. QStackedWidget implementation is written in the first class for which main.cpp instantiates the object and shows.
    4. To prevent high memory usage, I implement the class containing the QStackedWidget to dynamically create the window classes and delete them (as required). This is optional and will be based on your application's performance on your device.