Python newbie coming from Java (+SWT/Windowbuilder) and am having difficulty working out how to properly code a large desktop app in Python/Qt4(QtDesigner)/PySide.
I would like to keep any view logic in a controller class outside the .ui file (and it's .py conversion). Firstly as then the logic is independent of GUI framework and secondly as the .ui and resultant .py file get overwritten on any changes!.
Only examples I've found add action code to a monolithic MainWindow.py (generated from ui) or a MyForm.py (also generated from .ui). I can't see any way to link a POPO controller class to actions in QtDesigner.
Can anyone point me to workflows for creating a large scale application using QtDesigner in a scalable MVC/P methodology?
Firstly, just be aware that Qt already uses the concept of views and models but that's not actually what you're after. In short it's a way to automatically link a widget (e.g. a QListView) to a data source (e.g. a QStringListModel) so that changes to the data in the model automagically appear in the widget and vice versa. This is a useful feature but it's a different thing to an application scale MVC design, although the two can be used together and it does offer some obvious shortcuts. Application scale MVC design however must be manually programmed.
Here's an example MVC application that has a single view, controller, and model. The view has 3 widgets that each independently listen for and react to changes to data in the model. The spin box and button can both manipulate data in the model via the controller.
The file structure is arranged like this:
Application
mvc_app.py
would be responsible for instantiating each of the view, controllers, and model(s) and passing references between them. This can be quite minimal:Views
Use Qt designer to create the .ui layout files to the extent that you assign variables names to widgets and adjust their basic properties. Don't bother adding signals or slots as it's generally easier just to connect them to functions from within the view class.
The .ui layout files are converted to .py layout files when processed with pyuic or pyside-uic. The .py view files can then import the relevant auto-generated classes from the .py layout files.
The view class(es) should contain the minimal code required to connect to the signals coming from the widgets in your layout. View events can call and pass basic information to a method in the view class and onto a method in a controller class, where any logic should be. It would look something like:
The view doesn't do much apart from link widget events to the relevant controller function, and listen for changes in the model, which are emitted as Qt signals.
Controllers
The controller class(es) perform any logic and then sets data in the model. An example:
The
change_amount
function takes the new value from the widget, performs logic, and sets attributes on the model.Model
The model class stores program data and state and some minimal logic for announcing changes to this data. This model shouldn't be confused with the Qt model (see http://qt-project.org/doc/qt-4.8/model-view-programming.html) as it's not really the same thing.
The model might look like:
Writes to the model automatically emit signals to any listening views via code in the
setter
decorated functions. Alternatively the controller could manually trigger the signal whenever it decides.In the case where Qt model types (e.g. QStringListModel) have been connected with a widget then the view containing that widget does not need to be updated at all; this happens automatically via the Qt framework.
UI source file
For completion, the example
main_view.ui
file is included here:It is converted to
main_view_ui.py
by calling:The resource file
mvc_app.qrc
is converted tomvc_app_rc.py
by calling:Interesting links
Why Qt is misusing model/view terminology?