CuteHMI - CuteHMI (CuteHMI.2)
cutehmi::Worker Class Reference

Worker. More...

#include <cutehmi/Worker.hpp>

Inheritance diagram for cutehmi::Worker:
QObject

Classes

class  WorkEvent
 Work event. More...
 

Signals

void ready ()
 Results are ready. More...
 

Public Member Functions

 Worker (std::function< void()> task=nullptr)
 Default constructor. More...
 
 Worker (QThread &thread)
 Constructor. More...
 
 ~Worker () override
 
std::function< void()> task () const
 Get task. More...
 
void setTask (std::function< void()> task)
 Set task. More...
 
void wait () const
 Wait for the worker. More...
 
bool isReady () const
 Check if worker is ready. More...
 
bool isWorking () const
 Check if worker is working. More...
 
void employ (QThread &thread, bool start=true)
 Employ worker. More...
 
void work ()
 Do work. More...
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
 QObject (QObject *parent)
 
virtual ~QObject ()
 
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 QObjectListchildren () 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 ()
 
 Q_DISABLE_COPY (Class)
 
 Q_DISABLE_MOVE (Class)
 
 Q_DISABLE_COPY_MOVE (Class)
 
qobject_cast (QObject *object)
 
qobject_cast (const QObject *object)
 
qFindChild (const QObject *obj, const QString &name)
 
QList< T > qFindChildren (const QObject *obj, const QString &name)
 
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
 
 Q_CLASSINFO (Name, Value)
 
 Q_INTERFACES (...)
 
 Q_PROPERTY (...)
 
 Q_ENUMS (...)
 
 Q_FLAGS (...)
 
 Q_ENUM (...)
 
 Q_FLAG (...)
 
 Q_ENUM_NS (...)
 
 Q_FLAG_NS (...)
 
 Q_OBJECT Q_OBJECT
 
 Q_GADGET Q_GADGET
 
 Q_NAMESPACE Q_NAMESPACE
 
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 
 Q_SIGNALS Q_SIGNALS
 
 Q_SIGNAL Q_SIGNAL
 
 Q_SLOTS Q_SLOTS
 
 Q_SLOT Q_SLOT
 
 Q_EMIT Q_EMIT
 
 Q_INVOKABLE Q_INVOKABLE
 
 Q_REVISION Q_REVISION
 
 Q_SET_OBJECT_NAME (Object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 

Protected Member Functions

virtual void job ()
 Worker's job. More...
 
bool event (QEvent *event) override
 
- 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)
 
- Public Attributes inherited from QObject
typedef QObjectList
 
- Properties inherited from QObject
 objectName
 

Detailed Description

Worker.

This class acts as a container that allows specified code to be run in a specified thread. This class is useful when dealing with databases, because Qt SQL module does not allow for sharing database connections across threads. Thus if SQL connection is established in dedicated thread, a worker can run a task in that thread and send ready() signal when the task is finished.

Constructor & Destructor Documentation

◆ Worker() [1/2]

cutehmi::Worker::Worker ( std::function< void()>  task = nullptr)

Default constructor.

Parameters
tasktask function. See job().

◆ Worker() [2/2]

cutehmi::Worker::Worker ( QThread thread)

Constructor.

This constructor acts as if employ(thread, false) was called immediately after constructing default worker object.

Parameters
threadthread in which worker's job should be run.

◆ ~Worker()

cutehmi::Worker::~Worker ( )
override

Member Function Documentation

◆ employ()

void cutehmi::Worker::employ ( QThread thread,
bool  start = true 
)

Employ worker.

Employs worker in another thread. Internally this function moves worker object to the specified thread. This function imposes same restrictions as QObject::moveToThread().

Parameters
threadthread in which worker's job should be run.
startstart work. If start is set to true, then work() is called immediately.

◆ event()

bool cutehmi::Worker::event ( QEvent event)
overrideprotectedvirtual

Reimplemented from QObject.

◆ isReady()

bool cutehmi::Worker::isReady ( ) const

Check if worker is ready.

Returns
true when worker has completed the job, false otherwise.
Remarks
This method is thread-safe.

◆ isWorking()

bool cutehmi::Worker::isWorking ( ) const

Check if worker is working.

Returns
true if worker is processing its job, false otherwise.
Remarks
This method is thread-safe.

◆ job()

void cutehmi::Worker::job ( )
protectedvirtual

Worker's job.

This function is called when worker receives WorkEvent event. Use employ() function to run job in a specified thread. This function can be reimplemented. Default implementation calls task function if it has been set (it can be passed to the constructor or set via setTask() function). Default implementation does nothing if task has not been set (i.e. task = nullptr).

◆ ready

void cutehmi::Worker::ready ( )
signal

Results are ready.

This signal is emitted when the job() has been completed and the results are ready.

◆ setTask()

void cutehmi::Worker::setTask ( std::function< void()>  task)

Set task.

Parameters
tasktask function. See job().

◆ task()

std::function< void()> cutehmi::Worker::task ( ) const

Get task.

Returns
task function. See job().

◆ wait()

void cutehmi::Worker::wait ( ) const

Wait for the worker.

Causes calling thread to wait until worker finishes its job. Function does not block until work() has been called. It also won't block if worker has already finished its job.

Remarks
This method is thread-safe.

◆ work()

void cutehmi::Worker::work ( )

Do work.

This function posts WorkEvent, which tells worker to process the job() inside the thread in which it is employed.

Remarks
This method is thread-safe.