QtMvvm  1.0.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...
 

Public Member Functions

 ViewModel (QObject *parent=nullptr)
 Default constructor with parent.
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
virtual const QMetaObjectmetaObject () const
 
QString objectName () const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const
 
bool isWindowType () const
 
bool signalsBlocked () const
 
bool blockSignals (bool block)
 
QThreadthread () 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
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const
 
const QObjectList & children () 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
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const
 
bool disconnect (const QObject *receiver, const char *method) const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const
 
void dumpObjectInfo () const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const
 
QList< QByteArraydynamicPropertyNames () const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const
 
bool inherits (const char *className) 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
 
int senderSignalIndex () const
 
int receivers (const char *signal) const
 
bool isSignalConnected (const QMetaMethod &signal) 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);
See also
CoreApp::show, QTMVVM_INJECT

Definition at line 20 of file viewmodel.h.

Member Function Documentation

◆ 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::NetworkExchangeViewModel, and QtMvvm::SettingsViewModel.

◆ 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 66 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 73 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: