Open mottosso opened 4 years ago
Awesome post !
But I though the model
was the brain ? The thing that handles the business logic, and the control
is the nerves. Controls receive the user inputs and the model changes data by it's logic so the view represent model process result visually.
But I though the model was the brain ?
I've honestly never considered that. Whenever I read up on the topic, I typically conclude with "nobody knows" because there are so many variations on MVC (MVV, MVP, MV, what else?) and I've never fully understood how they differ. I've always turned to whatever Qt does, as it's at least documented and wouldn't result in a framework-on-framework.
But a quick search just now took me to a post I've read once before.
Except today it makes sense.
One of the things I've always struggled with is where to store the model.
So what ends up happening is the model is updated from the view, with data from the controller, which means it cannot be used/tested without a view, and that's bad.
Nuking the controller, and making the model the controller instead would make life a whole lot easier. The Qt docs even says so, which I honestly haven't noticed until just now; there is no C in MVC. I don't like the idea of mixing data with business logic; to me the model has always been just dumb "structured" data. Something facilitating a call to data()
and setData()
. And it feels a little backwards also having e.g. launchProcess()
and printSummary()
in there.
But let's try that, I think you're right.
Goal
Reduce duplication, simplify implementations of tools.
Motivation
Our "tools" are GUIs written in Qt, and GUIs are a hard problem to solve. There is no PEP 008 for writing graphical applications, and the result is a lack of consistency and foresight.
Without appropriate answers to these questions, extending or editing a graphical application is a problem made even harder.
Implementation
Let's see if we can run through these and establish a foundation upon which to build GUIs effectively. I'll write these as unambious as I can, but remember these are guidelines and though some are tried and true, some are not (I'll tag these appropriately).
Architecture
You generally won't need to divide an application into model, view and controller unless it is of a certain size. But as this is an article on consistency, let's move the goal posts to say "No application is small enough to not require MVC".
With MVC as our baseline, here's how to implement it.
view.py
control.py
model.py
These three are all that is required to implement an application. Additional modules are for convenience and organisation only, such as..
widgets.py
view.py
, yet specific to an application (i.e. not shared with anything else)util.py
delegates.py
Open Questions
Structure I - View
This involves the
def __init__()
inview.py
.Notes
pages
,panels
andwidgets
Window
MUST havetitle
declared as class attribute, used to dynamically change window title at run-time, e.g. to reflect a selection or stateNotes
QStackedWidget
to facilitate multiple pagesNotes
layout
; don't bother with unique names or maintaining reference to themNotes
on_
prefix, they are responding to an eventNotes
Notes
Notes
Structure II - Controller
A view merely interprets what the user wants, the controller is what actually makes it happen. When a button is pressed, a signal handler calls on the controller to take action.
Likewise, when the controller does something, the view is what tells the user about it.
Structure III - Model
...
Responsiveness
Always develop your application synchronously at first, blocking at every turn. Once a series of operations run stable but cause too much delay (200 ms+), consider parallelising and what should be happening from the users point of view.
Heads up
This got long. I'll pause here and update as I go. Potentially turn it into a more formal document, like CONTRIBUTING.md. Is this a good idea? Does this kind of thing help? Does it limit more than it liberates? Let me know.