Writing QML Application in a Flux way
-
Do you think it is beneficial to use Action-Dispatcher Design Pattern in C++ ?
If Yes, would you mind giving us an example in C++ ? -
@Vincent007 yes, I think so. However, I am not coding in pure C++ way. I could provide a C++ and QML mixing example code. Before it is ready, you may take a look about the C++ API of QuickFlux:
-
Someone implemented FLUX too!
-
@Vincent007 Interesting. thx.
I am happy to see more people talking about using Flux in Qt/QML even they are not using my library. That means the approach really works. Moreover, until now, I don't see a standard QML application guide available yet. I would like to draw more attention from Qt users / developers to think about this problem.
p.s I have added a FAQ about why it need a Dispatcher instead of just a QObject:
Why use AppDispatcher instead of listening from AppActions directly?
-
v1.0.4 has been released.
New Components
KeyTable
KeyTable is an object with properties equal to its key name. Once it's construction is completed, it will search all of its string property. If it is a string type and not assigned to any value, it will set its value by its name. It can be used to create ActionTypes.qml in QuickFlux Application.
Example:
KeyTable { // It will be set to "customField1" in Component.onCompleted callback. property string customField1; // Since it is already assigned a value, KeyTable will not modify this property. property string customField2 : "value"; }
Filter
Filter component listens for the parent's dispatched signal, if a dispatched signal match with its type, it will emit its own "dispatched" signal. Otherwise, it will simply ignore the signal.
This component provides an alternative way to filter incoming message which is suitable for making Store component.
Example:
pragma Singleton import QtQuick 2.0 import QuickFlux 1.0 import "../actions" AppListener { id: store property ListModel model: ListModel { } Filter { type: ActionTypes.addTask onDispatched: { model.append({task: message.task}); } } }
It is not suggested to use nested AppListener in a Store component. Because nested AppListener do not share the same AppListener::listenerId, it will be difficult to control the order of message reception between store component.
In contrast, Filter share the same listenerId with its parent, and therefore it is a solution for above problem.
Changes
AppScript
- Added "autoExit" property
examples/todo
- Migrated to use KeyTable and Filter
- Added StoreAdapter to demonstrate how to use "waitFor" between stores.
StoreAdapter.qml
import QtQuick 2.0 import "../stores" Item { Component.onCompleted: { TodoStore.waitFor = [UserPrefsStore.listenerId]; } }
-
QuickFlux v1.0.5 has been released.
New Component
ActionCreator
ActionCreator is a component that listens on its own signals, convert to message then dispatch via AppDispatcher. The message type will be same as the signal name. There has no limitation on number of arguments and data type.
For example, you may declare an ActionCreator based component as:
import QtQuick 2.0 import QuickFlux 1.0 pragma singleton ActionCreator { signal open(string url); }
It is equivalent to:
import QtQuick 2.0 import QuickFlux 1.0 pragma singleton Item { function open(url) { AppDispatcher.dispatch(“open”, {url: url}); } }
Changes
QFAppDispatcher
- Added dispatch(const QString &type, const QVariant& message) member method for C++ to dispatch message.
-
v1.0.6 has been released
New Component
- Object - It is a QtObject that able to hold children component.
Changes
1. Filter
a) Now it could listen from any component that has "dispatched" signal
Item { signal dispatched(string type, var message) Filter { type: ActionTypes.addItem onDispatched: {} } }
b) Support to hold children item
Filter { Promise { } }
c) Added "types" property to filter multiple types of action
Filter { types: [ActionTypes.addItem , ActionTypes.removeItem] }
2. ActionCreator
a) Added "genKeyTable()"
According to the registered signal in the ActionCreator object, it could create a key table (ActionTypes) in QML.
3. KeyTable
a) Added getSourceFile()/genHeaderFile() - Generate a C++ header and source file for this KeyTable object.
Bug Fix:
-
[QTBUG-58133] Crash on emitting a signal from C++ to QML with undefined QJSValue - Qt Bug Tracker
-
qmlRegisterType under MSVC goes nuts · Issue #7 · benlau/quickflux
Preview of Quick Flux 1.1
As a preview of new components in v1.1, they are also included in this release. But the API is not frozen. It may change in the official release.
- Dispatcher - non-singleton Dispatcher
- MiddlewareList / Middleware - a mechanism to extend the functionality of dispatcher allowing for advanced asynchronous workflow and integration with visual component like FileDialo
- Store - A replacement of AppListener
- Hydrate - Serialize and deserialize a store to/from a JSON object.
-
v1.1 has been released!
Detailed explanation:
What is new in Quick Flux 1.1?
New Features
-
Support Non-singleton dispatcher.
-
Middleware - a mechanism to extend the functionality of dispatcher allowing for advanced asynchronous workflow and integration with visual component like FileDialog.
-
Store - A replacement of AppListener that could listen from non-singleton dispatcher, and re-dispatch the action message to another Store components sequentially. It could avoid the over-complicated
waitFor
mechanism. -
Hydration - Serialize and deserialize a store to/from a QVariantMap object.
New Components
-
MiddlewareList & Middleware
-
Store
-
Hydrate
See Class Document for details
Bug Fix
https://bugreports.qt.io/browse/QTBUG-58133
Reference
-
-
I like the proposed architecture by Ben Lau a lot!
However, for small QML apps, you can also introduce a similar pattern with a smaller and lightweight solution purely in QML.
If you're interested, you can find the full developer guide here: https://v-play.net/apps/avoid-cpp-models-qt
Cheers,
GTDev