Multiple Windows - best practise
-
Hello all,
I've used Qt before but not applications using multiple windows & would appreciate some pointers.I'm developing an embedded Linux project that'll have a number of windows or views, each requiring a different background image:
- Startup
- Running (requires some animation & counter)
- Finished
- Maintenance (requires pin input)
My question is how should I best organise these different windows? Do I create different forms for each? If so should the main logic/ code be located in main.cpp that is then responsible for calling each form in turn?
Apologies for such a simple question but I appreciate you looking!
Leo
-
@leo738 Take a look at https://doc.qt.io/qt-5/qstackedwidget.html
You can use it to easily switch between different widgets. -
@leo738 said in Multiple Windows - best practise:
If so should the main logic/ code be located in main.cpp that is then responsible for calling each form in turn?
Your
int main()
function should have the event loop running. This at least forbids to run everything in turn from this function. You will need something derived fromQObject
(most of the time aQMainWindow
) which has slots reacting to signals. Since it is its own object I suggest that it has its own .h and .cpp. So, no – normally you should not have this code inmain.cpp
. -
This depends on how you want your "windows" to be laid out -- if you want to do like jsulm suggests, then it wouldn't be a matter of windows at all, just switching between widgets via. a stacked widget.
If you want your windows to be represented as actual windows, you will need some sort of window hierarchy; I don't recommend using multiple QMainWindows, but rather using non-modal QDialogs spawned with a QMainWindow as a parent. They can directly interact via. signals and slots, and they will all close when the main window is closed. Hope this helps.