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 | Static Public Member Functions | Protected Member Functions | List of all members
QtMvvm::CoreApp Class Referenceabstract

A logicaly application object to drive the mvvm application from the core part. More...

#include <coreapp.h>

+ Inheritance diagram for QtMvvm::CoreApp:

Public Slots

void bootApp ()
 Boots up the app and starts the mvvm presenting. More...
 

Signals

void appStarted (QPrivateSignal)
 Is emitted once the app has successfully started.
 

Public Member Functions

 CoreApp (QObject *parent=nullptr)
 Default Constructor.
 
void registerApp ()
 Registers this instance as the CoreApp instance. More...
 
- 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 ()
 

Static Public Member Functions

static CoreAppinstance ()
 Returns the current CoreApp instance.
 
static void disableAutoBoot ()
 Disables the automatic startup of the CoreApp. More...
 
template<typename TViewModel >
static void show (const QVariantHash &params={}, QPointer< ViewModel > parentViewModel=nullptr)
 Show a new ViewModel by its type. More...
 
static void show (const char *viewModelName, const QVariantHash &params={})
 Show a new ViewModel by its name. More...
 
static void show (const char *viewModelName, const QVariantHash &params, QPointer< ViewModel > parentViewModel)
 Show a new ViewModel by its name. More...
 
static void show (const QMetaObject *viewModelMetaObject, const QVariantHash &params={})
 Show a new ViewModel by its metaobject. More...
 
static void show (const QMetaObject *viewModelMetaObject, const QVariantHash &params, QPointer< ViewModel > parentViewModel)
 Show a new ViewModel by its metaobject. More...
 
static MessageResultshowDialog (const MessageConfig &config)
 Show a basic dialog. More...
 
static QVariant safeCastInputType (const QByteArray &type, const QVariant &value)
 Safely casts a value of the given edit type to the corresponding variant type. More...
 
template<typename T >
static void registerInputTypeMapping (const QByteArray &type)
 Register a type to be used as variant type for the given edit type. More...
 
static void registerInputTypeMapping (const QByteArray &type, int targetType)
 Register a type to be used as variant type for the given edit type. More...
 
static IPresenterpresenter ()
 Returns the currently used presenter.
 
- 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)
 

Protected Member Functions

virtual void performRegistrations ()
 Is called as part of the app registration, before any view setups. More...
 
virtual int startApp (const QStringList &arguments)=0
 Is called as part of the app start/boot, after all the view setups to start the app. More...
 
virtual void closeApp ()
 Is called when the app is about to quit. More...
 
bool autoParse (QCommandLineParser &parser, const QStringList &arguments)
 Can be used to automatically handle help and version commandline arguments. 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

- Properties inherited from QObject
 objectName
 

Detailed Description

A logicaly application object to drive the mvvm application from the core part.

Every QtMvvm Application needs a core application that performs the application setup and manages the ViewModels in order to present them. If you want to use QtMvvm, you need to perform the following steps:

  1. Create a custom App class in your core project and extend QtMvvm::CoreApp
  2. Implement the CoreApp::startApp method (and possibly other virtual methods)
  3. In your GUI Project, register the App by using the QTMVVM_REGISTER_CORE_APP macro

Simple example for a custom CoreApp:

class MyApp : public QtMvvm::CoreApp
{
Q_OBJECT
public:
explicit MyApp(QObject *parent = nullptr) :
{}
protected:
int startApp(const QStringList &arguments) override {
show<InitialViewModel>();
return EXIT_SUCCESS;
}
};
//optional: overwrite the coreApp macro:
#undef coreApp
#define coreApp static_cast<MyApp*>(QtMvvm::CoreApp::instance())

Then in the GUI project add to, for example, the main.cpp:

#include "myapp.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv); //registerApp() is called from here (and thus performRegistrations())
//...
return app.exec(); // bootApp() is called as soon as the eventloop starts (and thus startApp())
}

When using the app that way, a 3-step startup is performed:

  1. registerApp() and performRegistrations() are called as part of the QCoreApplication constructor as first thing to register types etc. from the core application.
  2. The rest of the main() in the GUI project runs to register further view related stuff and register additional services etc.
  3. The bootApp() and startApp() methods are called once the eventloop was entered in order to actually start the application and show the first viewmodels. Once started, the appStarted() signal is emitted as well.

Definition at line 20 of file coreapp.h.

Member Function Documentation

◆ autoParse()

QtMvvm::CoreApp::autoParse ( QCommandLineParser parser,
const QStringList arguments 
)
protected

Can be used to automatically handle help and version commandline arguments.

Parameters
parserA reference to the commandline parser to use
argumentsThe arguments to be parsed
Returns
true If parsing was successfull and you can continue starting the App

Use this method in your startApp() implementation in order to automatically handle special cases:

  • Errors
  • The Help option (--help)
  • The Version option (--version)

For those three cases false is returned and a dialog of the specific GUI shown to the user with either and error string, the help or the version information. To correctly use it, your startApp() function should look like this:

int MyApp::startApp(const QStringList &arguments)
{
parser.addVersionOption();
parser.addHelpOption();
//Add more options to setup the parser...
//shows help or version automatically
if(!autoParse(parser, arguments))
return EXIT_SUCCESS;
// actual startup code...
}
Note
It is important to return with EXIT_SUCCESS from the parser in order to actually show the dialogs to the user. Returning any other value would simply silently quit the application.
See also
CoreApp::startApp

◆ bootApp

QtMvvm::CoreApp::bootApp ( )
slot

Boots up the app and starts the mvvm presenting.

Attention
This method is called automatically by the QTMVVM_REGISTER_CORE_APP macro or the registerApp() method, unless it has been deactivated before with disableAutoBoot(). Unless you disabled the boot that way, don't call this method explicitly!

Internally it performs the actual start of the application and calls startApp() to start all services and show the initial viewmodels.

If startApp() returned successfully, appStarted() is emitted and thus the startup completed.

See also
QTMVVM_REGISTER_CORE_APP, CoreApp::startApp, CoreApp::disableAutoBoot, CoreApp::appStarted

◆ closeApp()

QtMvvm::CoreApp::closeApp ( )
protectedvirtual

Is called when the app is about to quit.

Is connected to the QCoreApplication::aboutToQuit signal and is called to clean up your application. Using this method is more convenient as it is only called in case the startup was successfull (returned EXIT_SUCCESS).

See also
CoreApp::startApp

◆ disableAutoBoot()

QtMvvm::CoreApp::disableAutoBoot ( )
static

Disables the automatic startup of the CoreApp.

In case you don't want your application be be started automatically whenQCoreApplication::exec is called, you must call this method before The QCoreApplication (or any other application) constructor is called:

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
//...
}

In order to eventually start the app, simply call bootApp().

See also
CoreApp::bootApp, QTMVVM_REGISTER_CORE_APP, CoreApp::registerApp

◆ performRegistrations()

QtMvvm::CoreApp::performRegistrations ( )
protectedvirtual

Is called as part of the app registration, before any view setups.

Is called by registerApp() (and thus automatically when using the QTMVVM_REGISTER_CORE_APP macro) in order to perform initial registrations of types etc. before any such code is beeing executed for the gui projects.

See also
QTMVVM_REGISTER_CORE_APP, CoreApp::registerApp, CoreApp::startApp

◆ registerApp()

QtMvvm::CoreApp::registerApp ( )

Registers this instance as the CoreApp instance.

Attention
This method is called automatically by the QTMVVM_REGISTER_CORE_APP macro. If you use the macro, don't call this method explicitly!

Internally it performs some initial registrations, calls performRegistrations() and then schedules the automatic call of bootApp() if not disabled.

See also
QTMVVM_REGISTER_CORE_APP, CoreApp::performRegistrations, CoreApp::bootApp, CoreApp::disableAutoBoot

◆ registerInputTypeMapping() [1/2]

template<typename T >
QtMvvm::CoreApp::registerInputTypeMapping ( const QByteArray type)
static

Register a type to be used as variant type for the given edit type.

Template Parameters
TThe C++ type to be registered for the given edit type
Parameters
typeThe edit type to register the C++ type for

Registeres the given type, so that whenever type is passed to safeCastInputType(), the given variant will be converter to T

See also
CoreApp::safeCastInputType

Definition at line 98 of file coreapp.h.

◆ registerInputTypeMapping() [2/2]

QtMvvm::CoreApp::registerInputTypeMapping ( const QByteArray type,
int  targetType 
)
static

Register a type to be used as variant type for the given edit type.

Parameters
typeThe edit type to register the C++ type for
targetTypeThe id of C++ type to be registered for the given edit type

Registeres the given type, so that whenever type is passed to safeCastInputType(), the given variant will be converter to targetType

See also
CoreApp::safeCastInputType

◆ safeCastInputType()

QtMvvm::CoreApp::safeCastInputType ( const QByteArray type,
const QVariant value 
)
static

Safely casts a value of the given edit type to the corresponding variant type.

Parameters
typeThe edit type the given value is supposed to be of
valueThe value to be cast to the type specified by the edit type
Returns
A variant of the given value, cast to that type

The primary function of this method is to cast types like url to their respective C++ equivalent, i.e. QUrl. The table below shows the default type conversion map, and custom entries can be added via registerInputTypeMapping(). For all types not in that mapping, the function tries to see type like a C++ typename, i.e. int and use that to convert the variant. If no type can be determined, the value is returned without doing anything.

Edit-Type C++-Type
switch bool
string QString
number double
range int
date QDateTime
color QColor
url QUrl
var QVariant
variant QVariant
selection QVariant
list QVariant
radiolist QVariant
See also
CoreApp::registerInputTypeMapping

◆ show() [1/5]

template<typename TViewModel >
QtMvvm::CoreApp::show ( const QVariantHash &  params = {},
QPointer< ViewModel parentViewModel = nullptr 
)
inlinestatic

Show a new ViewModel by its type.

Template Parameters
TViewModelThe type of the viewmodel to be presented
Parameters
paramsA map of named parameters to be passed to the viewmodel
parentViewModelThe viewmodel to be used as logical parent to the new one, for presenting

Creates a new instance of the Viewmodel class, creates to corresponding view and then shows it. This is done via the IPresenter that is currently registered.

See also
ViewModel::show, CoreApp::showDialog

Definition at line 91 of file coreapp.h.

◆ show() [2/5]

static void QtMvvm::CoreApp::show ( const char *  viewModelName,
const QVariantHash &  params = {} 
)
static

Show a new ViewModel by its name.

Parameters
viewModelNameThe name of the viewmodel to be presented
paramsA map of named parameters to be passed to the viewmodel
parentViewModelThe viewmodel to be used as logical parent to the new one, for presenting

Creates a new instance of the Viewmodel class, creates to corresponding view and then shows it. This is done via the IPresenter that is currently registered, taking the parent viewmodel into consideration where neccessary.

See also
ViewModel::show, CoreApp::showDialog

◆ show() [3/5]

QtMvvm::CoreApp::show ( const char *  viewModelName,
const QVariantHash &  params,
QPointer< ViewModel parentViewModel 
)
static

Show a new ViewModel by its name.

Parameters
viewModelNameThe name of the viewmodel to be presented
paramsA map of named parameters to be passed to the viewmodel
parentViewModelThe viewmodel to be used as logical parent to the new one, for presenting

Creates a new instance of the Viewmodel class, creates to corresponding view and then shows it. This is done via the IPresenter that is currently registered, taking the parent viewmodel into consideration where neccessary.

See also
ViewModel::show, CoreApp::showDialog

◆ show() [4/5]

static void QtMvvm::CoreApp::show ( const QMetaObject viewModelMetaObject,
const QVariantHash &  params = {} 
)
static

Show a new ViewModel by its metaobject.

Parameters
viewModelMetaObjectThe metaobject of the viewmodel to be presented
paramsA map of named parameters to be passed to the viewmodel
parentViewModelThe viewmodel to be used as logical parent to the new one, for presenting

Creates a new instance of the Viewmodel class, creates to corresponding view and then shows it. This is done via the IPresenter that is currently registered, taking the parent viewmodel into consideration where neccessary.

See also
ViewModel::show, CoreApp::showDialog

◆ show() [5/5]

QtMvvm::CoreApp::show ( const QMetaObject viewModelMetaObject,
const QVariantHash &  params,
QPointer< ViewModel parentViewModel 
)
static

Show a new ViewModel by its metaobject.

Parameters
viewModelMetaObjectThe metaobject of the viewmodel to be presented
paramsA map of named parameters to be passed to the viewmodel
parentViewModelThe viewmodel to be used as logical parent to the new one, for presenting

Creates a new instance of the Viewmodel class, creates to corresponding view and then shows it. This is done via the IPresenter that is currently registered, taking the parent viewmodel into consideration where neccessary.

See also
ViewModel::show, CoreApp::showDialog

◆ showDialog()

QtMvvm::CoreApp::showDialog ( const MessageConfig config)
static

Show a basic dialog.

Parameters
configThe configuration of the message to be shown
Returns
A message result to keep track of the shown dialog

The configuration is passed to the presenter in order to show a simple dialog. You can use the returned object to monitor the dialog and receive the result.

Note
By default, the returned object will delete itself automatically once the dialog has been closed.
See also
CoreApp::show, MessageConfig, MessageResult

◆ startApp()

QtMvvm::CoreApp::startApp ( const QStringList arguments)
protectedpure virtual

Is called as part of the app start/boot, after all the view setups to start the app.

Parameters
argumentsThe command line arguments that have been passed to the application
Returns
An exit code as a result of the startup code

Is called by bootApp() (and thus automatically when using the QTMVVM_REGISTER_CORE_APP macro) in order to start the app logic and show initial viewmodels. Return EXIT_SUCCESS (0) to indicate the start was successful. If any other value is returned, the application exits with that value as return code.

See also
CoreApp::autoParse, QTMVVM_REGISTER_CORE_APP, CoreApp::bootApp, CoreApp::closeApp, CoreApp::performRegistrations

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