It is theoretically possible to replace one UI with another.
You will need to follow the following steps:
- Destroy all the child objects created by the first UI in the constructor
- Create an instance of the new UI
- Run
setupUi(this)
on the new UI
As I said, this is theoretical. It's is far from practical though because your UI classes will have to be derived from the same base class and all slots relating to your first UI might behave strangely if connected to your new UI.
What I could suggest is that you follow the Model-View-Controller (MVC) design pattern. Here's one suggestion, others may have better ones.
- Create a derived class of
QWidget
to function as yourController
class. Design all the signals and slots that will handle all the user events generated by theView
. - Design the UI in QtDesigner and make this a private member of your
Controller
class. Let is build all the UI elements in the Controller class' constructor and then connect all the signals and slots. This will function as theView
in the design pattern. - If you need some persistent data, either create a class or collection of classes to function as the
Model
, or pass one to yourController
class. Use this to set all values of child widgets in yourView
UI. - Pass the
Controller
QWidget to theQMainWindow
functionsetCentralWidget()
to set it as the UI that the user can see. - Repeat steps 1-4 for each different UI that you might need.
- When you need to swap out UIs, just use the
setCentralWidget()
onQMainWindow
to change to the appropriate UI.