QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
Public Slots | Signals | Public Member Functions | Protected Member Functions | List of all members
QtMvvm::ViewModel Class Reference

The base class for all viewmodels. More...

#include <viewmodel.h>

+ Inheritance diagram for QtMvvm::ViewModel:

Public Slots

virtual void onInit (const QVariantHash &params)
 Called by the presenter to initialize the viewmodel. More...
 
virtual void onResult (quint32 requestCode, const QVariant &result)
 Called by the presenter when a result of a showed viewmodel is ready. More...
 

Signals

void resultReady (const QVariant &result)
 Should be emitted when the viewmodels result is ready. More...
 
QTMVVM_REVISION_1 void instanceInvoked (const QVariantHash &params, QPrivateSignal)
 Is emitted on single instance viewmodels when they get shown again. More...
 

Public Member Functions

 ViewModel (QObject *parent=nullptr)
 Default constructor with parent.
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Protected Member Functions

template<typename TViewModel >
void show (const QVariantHash &params={}) const
 Show another viewmodel as a child of this one. More...
 
void show (const char *viewModelName, const QVariantHash &params={}) const
 Show another viewmodel as a child of this one. More...
 
void show (const QMetaObject *viewMetaObject, const QVariantHash &params={}) const
 Show another viewmodel as a child of this one. More...
 
template<typename TViewModel >
void showForResult (quint32 requestCode, const QVariantHash &params={}) const
 Show another viewmodel as a child of this one and expect its result. More...
 
void showForResult (quint32 requestCode, const char *viewModelName, const QVariantHash &params={}) const
 Show another viewmodel as a child of this one and expect its result. More...
 
void showForResult (quint32 requestCode, const QMetaObject *viewMetaObject, const QVariantHash &params={}) const
 Show another viewmodel as a child of this one and expect its result. More...
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The base class for all viewmodels.

The ViewModel is the primary class of the core library is must be implemented to create custom viewmodels to be shown via the mvvm mechanism. To create a custom viewmodel, simply implement this class and show the viewmodel via one of the show methods. Viewmodels presented that way support automatic injection via injectable properties.

Attention
In order for a viewmodel be showble, it must implement an invokable constructor with a single QObject* parameter for the parent. It basically should look like this:
Q_INVOKABLE explicit MyViewModel(QObject *parent = nullptr);

"Singleton Viewmodels"

Normally, showing a viewmodel will always create a new instance of it, and thus show a new gui. However, by adding the QTMVVM_SINGLETON macro to the class definition, your viewmodel becomes a single instance. This means, it will only be created once, and subsequent show requests will instead only trigger the instanceInvoked() signal on the existing instance.

If the existing instance gets destroyed at some point, show requests will show a new instance.

"Container Viewmodels"

Some viewmodel require a special viewmodel as their parent to be show in. To simplify the handling of those viewmodel, you can specify a parent container for a viewmodel via the QTMVVM_CONTAINER_VM macro. If added, everytime you try to show a viewmodel of this kind, the presenter will first create a new viewmodel of the type specified by the macro, and then show the original viewmodel immediatly after as child of the newly created parent. (The parent and the child can both be singletons)

You can specify parameters to be passed to the parent that is to be created via a special parameter named "qtmvvm_container_params". The value of that property must be a variant hash as well. In addition, a special parameter is added to the parent parameter, the "qtmvvm_container_for" parameter, which contains the name of the viewmodel that triggered its creation.

See also
CoreApp::show, QTMVVM_INJECT, QTMVVM_SINGLETON, QTMVVM_CONTAINER_VM

Definition at line 21 of file viewmodel.h.

Member Function Documentation

◆ instanceInvoked

QtMvvm::ViewModel::instanceInvoked ( const QVariantHash &  params,
QPrivateSignal   
)
signal

Is emitted on single instance viewmodels when they get shown again.

Parameters
paramsThe show parameters that were passed to the show method

If this viewmodel is a single instance viewmodel (QTMVVM_SINGLETON), then only the first show will actually create a viewmodel. All subsequent shows for that type will instead trigger this signal on the existing instance.

See also
QTMVVM_SINGLETON, "Singleton Viewmodels"

◆ onInit

QtMvvm::ViewModel::onInit ( const QVariantHash &  params)
virtualslot

Called by the presenter to initialize the viewmodel.

Parameters
paramsThe parameters to initialize the viewmodel with

This method is called by the presenter right after creating the view and reparenting the viewmodel to the view. The parameters are the ones that have been passed to the show method called to show this viewmodel instance. Reimplement this method if you need to perform initializations after beeing assigned to a viewmodel or if you want to support a parametrized viewmodel.

See also
ViewModel::show, ViewModel::showForResult, CoreApp::show

Reimplemented in QtMvvm::DataSyncViewModel, QtMvvm::SettingsViewModel, QtMvvm::NetworkExchangeViewModel, and QtMvvm::DataSyncSettingsViewModel.

◆ onResult

QtMvvm::ViewModel::onResult ( quint32  requestCode,
const QVariant result 
)
virtualslot

Called by the presenter when a result of a showed viewmodel is ready.

Parameters
requestCodeThe request code of the show request for the viewmodel that triggered the result
resultThe result passed from the viewmodel

When showing a child viewmodel via showForResult(), then the result of that show request is reported back via this function. The requestCode is the one that was passed to the showForResult() method, and the result what the viewmodel reported back. If the showed viewmodel emitted resultReady() before beeing destroyed, this value passed to that signal is whats reported as result. If the child viewmodel gets destroyed without ever emitting that signal, this method is still called, but with an invalid QVariant as result.

See also
ViewModel::showForResult, ViewModel::resultReady

Reimplemented in QtMvvm::DataSyncViewModel, and QtMvvm::NetworkExchangeViewModel.

◆ resultReady

QtMvvm::ViewModel::resultReady ( const QVariant result)
signal

Should be emitted when the viewmodels result is ready.

Parameters
resultThe result that should be passed to the parent viewmodel

Viewmodels that have been created via showForResult() must emit this signal to report back the result of the show request. Doing so will lead to the onResult() method of the showing viewmodel beeing with the emitted result as second parameter. Not emitting this signal before the viewmodel gets destroy leads to the onResult() beeing called with an invalid result.

See also
ViewModel::showForResult, ViewModel::onResult

◆ show() [1/3]

template<typename TViewModel >
QtMvvm::ViewModel::show ( const QVariantHash &  params = {}) const
inlineprotected

Show another viewmodel as a child of this one.

Parameters
paramsThe show parameters to be passed to the created viewmodel

This method will send a show request to the core app to show a viewmodel of the given type. The parameters are passed to the onInit() method by the presenter after creating and parenting the view. The viewmodel will be shown asynchronously, so this method will return immediatly.

See also
ViewModel::showForResult, ViewModel::onInit, CoreApp::show

Definition at line 77 of file viewmodel.h.

◆ show() [2/3]

QtMvvm::ViewModel::show ( const char *  viewModelName,
const QVariantHash &  params = {} 
) const
protected

Show another viewmodel as a child of this one.

Parameters
viewModelNameThe name of the viewmodel class to be shown
paramsThe show parameters to be passed to the created viewmodel

This method will send a show request to the core app to show a viewmodel of the given type. The parameters are passed to the onInit() method by the presenter after creating and parenting the view. The viewmodel will be shown asynchronously, so this method will return immediatly.

See also
ViewModel::showForResult, ViewModel::onInit, CoreApp::show

◆ show() [3/3]

QtMvvm::ViewModel::show ( const QMetaObject viewMetaObject,
const QVariantHash &  params = {} 
) const
protected

Show another viewmodel as a child of this one.

Parameters
viewMetaObjectThe metaobject of the viewmodel class to be shown
paramsThe show parameters to be passed to the created viewmodel

This method will send a show request to the core app to show a viewmodel of the given type. The parameters are passed to the onInit() method by the presenter after creating and parenting the view. The viewmodel will be shown asynchronously, so this method will return immediatly.

See also
ViewModel::showForResult, ViewModel::onInit, CoreApp::show

◆ showForResult() [1/3]

template<typename TViewModel >
QtMvvm::ViewModel::showForResult ( quint32  requestCode,
const QVariantHash &  params = {} 
) const
inlineprotected

Show another viewmodel as a child of this one and expect its result.

Parameters
requestCodeThe code of the show request
paramsThe show parameters to be passed to the created viewmodel

This method will send a show request to the core app to show a viewmodel of the given type. The parameters are passed to the onInit() method by the presenter after creating and parenting the view. The viewmodel will be shown asynchronously, so this method will return immediatly. The viewmodel is show for a result, meaning that a result is reported back via onInit() as soon as the shown viewmodel emits resultReady() or has been destroyed. The request code is passed to the onResult() method in order to identify the show request.

See also
ViewModel::show, ViewModel::onInit, ViewModel::resultReady, ViewModel::onResult, CoreApp::show

Definition at line 84 of file viewmodel.h.

◆ showForResult() [2/3]

QtMvvm::ViewModel::showForResult ( quint32  requestCode,
const char *  viewModelName,
const QVariantHash &  params = {} 
) const
protected

Show another viewmodel as a child of this one and expect its result.

Parameters
viewModelNameThe name of the viewmodel class to be shown
requestCodeThe code of the show request
paramsThe show parameters to be passed to the created viewmodel

This method will send a show request to the core app to show a viewmodel of the given type. The parameters are passed to the onInit() method by the presenter after creating and parenting the view. The viewmodel will be shown asynchronously, so this method will return immediatly. The viewmodel is show for a result, meaning that a result is reported back via onInit() as soon as the shown viewmodel emits resultReady() or has been destroyed. The request code is passed to the onResult() method in order to identify the show request.

See also
ViewModel::show, ViewModel::onInit, ViewModel::resultReady, ViewModel::onResult, CoreApp::show

◆ showForResult() [3/3]

QtMvvm::ViewModel::showForResult ( quint32  requestCode,
const QMetaObject viewMetaObject,
const QVariantHash &  params = {} 
) const
protected

Show another viewmodel as a child of this one and expect its result.

Parameters
viewMetaObjectThe metaobject of the viewmodel class to be shown
requestCodeThe code of the show request
paramsThe show parameters to be passed to the created viewmodel

This method will send a show request to the core app to show a viewmodel of the given type. The parameters are passed to the onInit() method by the presenter after creating and parenting the view. The viewmodel will be shown asynchronously, so this method will return immediatly. The viewmodel is show for a result, meaning that a result is reported back via onInit() as soon as the shown viewmodel emits resultReady() or has been destroyed. The request code is passed to the onResult() method in order to identify the show request.

See also
ViewModel::show, ViewModel::onInit, ViewModel::resultReady, ViewModel::onResult, CoreApp::show

The documentation for this class was generated from the following files: