Adventures in PySide6 MVVM - Part 1
Continuing my journey on Qt and PyQt, I was interested in how to structure medium to large sized applications with this technology (and Desktop applications for Python in general). I came across some blogs about Python with MVC, MVP and MVVM but nothing substantial in larger examples. I also discovered that PySide6 may be the better way to go due to license and official support.
What is MVVM?
I was first exposed to the Model-View-ViewModel (MVVM) pattern while working in C# and WPF years ago and has felt like the more modular and maintainable of the three. Per Microsoft (https://learn.microsoft.com/en-us/dotnet/architecture/maui/mvvm).
There are three core components in the MVVM pattern: the model, the view, and the view model. Each serves a distinct purpose. The diagram below shows the relationships between the three components.
Summary Comparison
MVC: Emphasizes separation of concerns with distinct roles for Model, View, and Controller. Common in web development.
MVP: Focuses on testability and a clear separation between UI representation (View) and UI logic (Presenter). Widely used in desktop and mobile applications.
MVVM: Utilizes data binding to create a direct connection between the View and ViewModel, simplifying UI logic and improving maintainability. Popular in applications with data-binding frameworks.
Application Overview
In this article, we will build a simple Desktop application using PySide6 widgets, with a simple list on the left pane and a main content area on the right. The corresponding view is displayed in the main content area when you select the list item on the left.
File Structure
I chose to create sub-packages for model, view and viewmodel sections to keep organized in some manner. I am running "main.py" in debug mode to launch.
Components
The following is a summary of the items developed within this application.
Menu bar and Toolbar
This demonstrates the basic file menu. One notable element is the use of "qtawesome" for the icons, which made it painless to include.
PySide6 uses QActions which make it
About Dialog
A simple modal prompt as a makeshift about box.
Themes
PySide6 supports themes. This application provides a simple example of both light and dark themes. For the moment, this is hard-coded, but intent is to tie the setting to the "Preferences" dialog.
Preferences Dialog
Views
Drive Simulator
A simple representation of a motor drive and pid controller to demonstrate use of QChart and basic controls.
Rich Textbox Editing
Demonstrates basic use of QTextEdit with common toolbar.
Dummy
The simplest MVVM component.
Code
Repository: https://github.com/ericjameszimmerman/pyside6-mvvm-example
v0.1.0: https://github.com/ericjameszimmerman/pyside6-mvvm-example/releases/tag/0.1.0
Subscribe to my newsletter
Read articles from Eric Zimmerman directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Eric Zimmerman
Eric Zimmerman
As a seasoned software engineering professional with over two decades of experience, I specialize in leading and architecting complex software projects.