What is the best method to create a multi level GUI windows?
-
I want to create three levels of GUI windows. For example,
Level 1 : Main Window with Button lables - "Living Beings" and "Non living Beings".
Level 2: On clicking "Living Beings" I get another window where I have buttons with list of living beings such as "Human", "Elephant", "Tiger".
Level 3: On clicking "Human" button I must get another window which has basic information about "Human".Now, I can implement this multi level GUI windows in two ways:
- All the window objects are created in the "main()" function and accordingly the objects are "show()ed".
- Objects of Level two are created in the constructors of Level one object windows. Objects of Level three are created in the constructors of Level two windows. This way I can free the objects of current level through the destructors of previous levels.
Which one would be suggested during the implementation and why?
-
Hi,
It really depends on the purpose of your widgets and application. In such a simple case, you can do everything within your QMainWindow derived object.
How you split things in your application will highly depend on what it does, what each of your widget is responsible for, how you want them to be testable, etc.
There's no one solution fits all situations.
-
@activeLearner
There are many ways to do it-
- StackedWidgets
It is simplest way to do the things that you have said. It may take memory but it is easiest method to solve this.
How it works
First Create buttons
Add stacked widgets then you can create specific widgets such as human elephant in living beings. (You can promote widgets in ui too)
Again you can add another stacked widgets inside that widget so it's most easy way to do so
https://doc.qt.io/qt-5/qstackedwidget.html
- StackedWidgets
-
- Simple dialogs || windows
You can create windows on clicking the buttons and again inside it you can do same and achieve it. I don't recommend it as it looks so untidy
- Simple dialogs || windows
- 3.tabWidget
It's similar to stacked widgets but it looks more like your browser tabs
You can create tabWidget inside it and do same for more deep
https://doc.qt.io/qt-5/qtwidgets-dialogs-tabdialog-example.html - 4.Simple Widgets
Another approach to get this is you can simply create widgets and
use setVisible property to show on the basis of user clicks
There is nothing that is best option. I like stackedWidget the most and can handle almost all things for simple development
-