QCA::SyncThread

Search for usage in LXR

#include <QtCrypto>

Inheritance diagram for QCA::SyncThread:

Public Member Functions

 SyncThread (QObject *parent=nullptr)
 
 ~SyncThread () override
 
QVariant call (QObject *obj, const QByteArray &method, const QVariantList &args=QVariantList(), bool *ok=nullptr)
 
void start ()
 
void stop ()
 
- Public Member Functions inherited from QThread
 QThread (QObject *parent)
 
virtual bool event (QEvent *event) override
 
QAbstractEventDispatchereventDispatcher () const const
 
void exit (int returnCode)
 
void finished ()
 
bool isFinished () const const
 
bool isInterruptionRequested () const const
 
bool isRunning () const const
 
int loopLevel () const const
 
QThread::Priority priority () const const
 
void quit ()
 
void requestInterruption ()
 
void setEventDispatcher (QAbstractEventDispatcher *eventDispatcher)
 
void setPriority (QThread::Priority priority)
 
void setStackSize (uint stackSize)
 
uint stackSize () const const
 
void start (QThread::Priority priority)
 
void started ()
 
void terminate ()
 
bool wait (QDeadlineTimer deadline)
 
bool wait (unsigned long time)
 
- Public Member Functions inherited from QObject
 QObject (QObject *parent)
 
bool blockSignals (bool block)
 
const QObjectListchildren () const const
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectInfo ()
 
void dumpObjectInfo () const const
 
void dumpObjectTree ()
 
void dumpObjectTree () const const
 
QList< QByteArraydynamicPropertyNames () const const
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
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
 
bool inherits (const char *className) const const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const const
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
QVariant property (const char *name) const const
 
 Q_CLASSINFO (Name, Value)
 
 Q_DISABLE_COPY (Class)
 
 Q_DISABLE_COPY_MOVE (Class)
 
 Q_DISABLE_MOVE (Class)
 
 Q_EMIT Q_EMIT
 
 Q_ENUM (...)
 
 Q_ENUM_NS (...)
 
 Q_ENUMS (...)
 
 Q_FLAG (...)
 
 Q_FLAG_NS (...)
 
 Q_FLAGS (...)
 
 Q_GADGET Q_GADGET
 
 Q_INTERFACES (...)
 
 Q_INVOKABLE Q_INVOKABLE
 
 Q_NAMESPACE Q_NAMESPACE
 
 Q_NAMESPACE_EXPORT (EXPORT_MACRO)
 
 Q_OBJECT Q_OBJECT
 
 Q_PROPERTY (...)
 
 Q_REVISION Q_REVISION
 
 Q_SET_OBJECT_NAME (Object)
 
 Q_SIGNAL Q_SIGNAL
 
 Q_SIGNALS Q_SIGNALS
 
 Q_SLOT Q_SLOT
 
 Q_SLOTS Q_SLOTS
 
qFindChild (const QObject *obj, const QString &name)
 
QList< T > qFindChildren (const QObject *obj, const QRegExp &regExp)
 
QList< T > qFindChildren (const QObject *obj, const QString &name)
 
qobject_cast (QObject *object)
 
qobject_cast (const QObject *object)
 
 QT_NO_NARROWING_CONVERSIONS_IN_CONNECT QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const const
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
QThreadthread () const const
 

Protected Member Functions

virtual void atEnd ()=0
 
virtual void atStart ()=0
 
void run () override
 
- Protected Member Functions inherited from QThread
int exec ()
 
- Protected Member Functions inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
int receivers (const char *signal) const const
 
QObjectsender () const const
 
int senderSignalIndex () const const
 
virtual void timerEvent (QTimerEvent *event)
 

Related Functions

(Note that these are not member functions.)

QCA_EXPORT bool invokeMethodWithVariants (QObject *obj, const QByteArray &method, const QVariantList &args, QVariant *ret, Qt::ConnectionType type=Qt::AutoConnection)
 
QCA_EXPORT int methodReturnType (const QMetaObject *obj, const QByteArray &method, const QList< QByteArray > &argTypes)
 

Additional Inherited Members

- Public Types inherited from QThread
enum  Priority
 
- Properties inherited from QObject
 objectName
 
- Static Public Member Functions inherited from QThread
QThreadcreate (Function &&f, Args &&...args)
 
QThreadcreate (Function &&f)
 
QThreadcurrentThread ()
 
Qt::HANDLE currentThreadId ()
 
int idealThreadCount ()
 
void msleep (unsigned long msecs)
 
void sleep (unsigned long secs)
 
void usleep (unsigned long usecs)
 
void yieldCurrentThread ()
 
- Static Public Member Functions inherited from QObject
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 QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
- Public Attributes inherited from QThread
 HighestPriority
 
 HighPriority
 
 IdlePriority
 
 InheritPriority
 
 LowestPriority
 
 LowPriority
 
 NormalPriority
 
 TimeCriticalPriority
 
- Public Attributes inherited from QObject
typedef QObjectList
 
- Static Protected Member Functions inherited from QThread
void setTerminationEnabled (bool enabled)
 

Detailed Description

Convenience class to run a thread and interact with it synchronously.

SyncThread makes it easy to perform the common practice of starting a thread, running some objects in that thread, and then interacting with those objects safely. Often, there is no need to directly use threading primitives (e.g. QMutex), resulting in very clean multi-threaded code.

Note
The following is an excerpt from http://delta.affinix.com/2006/11/13/synchronized-threads-part-3/


With SyncThread, you can start, stop, and call a method in another thread while the main thread sleeps. The only requirement is that the methods be declared as slots.

Below is a contrived example, where we have an object in another thread that increments a counter over a some interval, using the Qt event loop, and provides a method to inspect the value.

First, the Counter object:

class Counter : public QObject
{
private:
int x;
QTimer timer;
public:
Counter() : timer(this)
{
x = 0;
connect(&timer, &QTimer::timeout, this, &Counter::t_timeout);
}
public slots:
void start(int seconds)
{
timer.setInterval(seconds * 1000);
timer.start();
}
int value() const
{
return x;
}
private Q_SLOTS:
void t_timeout()
{
++x;
}
};

Looks like a typical object, no surprises.

Now to wrap Counter with SyncThread. We went over how to do this in the first article, and it is very straightforward:

class CounterThread : public SyncThread
{
public:
Counter *counter;
CounterThread(QObject *parent) : SyncThread(parent)
{
counter = 0;
}
~CounterThread()
{
// SyncThread will stop the thread on destruct, but since our
// atStop() function makes references to CounterThread's
// members, we need to shutdown here, before CounterThread
// destructs.
stop();
}
protected:
virtual void atStart()
{
counter = new Counter;
}
virtual void atStop()
{
delete counter;
}
};

We can then use it like this:

CounterThread *thread = new CounterThread;
// after this call, the thread is started and the Counter is ready
thread->start();
// let's start the counter with a 1 second interval
thread->call(thread->counter, "start", QVariantList() << 1);
...
// after some time passes, let's check on the value
int x = thread->call(thread->counter, "value").toInt();
// we're done with this thing
delete thread;

Even without the call() function, SyncThread is still very useful for preparing objects in another thread, which you can then QObject::connect() to and use signals and slots like normal.

Definition at line 279 of file qca_support.h.

Constructor & Destructor Documentation

QCA::SyncThread::SyncThread ( QObject parent = nullptr)

Standard constructor.

Parameters
parentthe parent object for this parent.
QCA::SyncThread::~SyncThread ( )
override

Calls stop() and then destructs.

Note
Subclasses should call stop() in their own destructor

Member Function Documentation

virtual void QCA::SyncThread::atEnd ( )
protectedpure virtual

Reimplement this to perform your deinitialization.

virtual void QCA::SyncThread::atStart ( )
protectedpure virtual

Reimplement this to perform your initialization.

QVariant QCA::SyncThread::call ( QObject obj,
const QByteArray method,
const QVariantList &  args = QVariantList(),
bool *  ok = nullptr 
)

Calls a slot of an object in the thread.

This function will block until the slot has returned.

It is possible for the call to fail, for example if the method does not exist.

The arguments and return value of the call use QVariant. If the method has no return value (returns void), then the returned QVariant will be null.

Parameters
objthe object to call the method on
methodthe name of the method (without the arguments or brackets)
argsthe list of arguments to use in the method call
okif not 0, true is stored here if the call succeeds, otherwise false is stored here.
void QCA::SyncThread::run ( )
overrideprotectedvirtual

Starts the event loop and calls atStart and atStop as necessary.

Reimplemented from QThread.

void QCA::SyncThread::start ( )

Starts the thread, begins the event loop the thread, and then calls atStart() in the thread.

This function will block until atStart() has returned.

void QCA::SyncThread::stop ( )

Stops the event loop of the thread, calls atStop() in the thread, and instructs the thread to finish.

This function will block until the thread has finished.

Friends And Related Function Documentation

QCA_EXPORT bool invokeMethodWithVariants ( QObject obj,
const QByteArray method,
const QVariantList &  args,
QVariant ret,
Qt::ConnectionType  type = Qt::AutoConnection 
)
related

Convenience method to invoke a method by name, using a variant list of arguments.

This function can be used as shown:

class TestClass : public QObject
{
// ...
public slots:
QString qstringMethod() { return QString( "the result" ); };
bool boolMethod( const QString & ) { return true; };
};
TestClass *testClass = new TestClass;
QVariantList args;
QVariant stringRes;
// calls testClass->qstringMethod() with no arguments ( since args is an empty list)
bool ret = QCA::invokeMethodWithVariants( testClass, QByteArray( "qstringMethod" ), args, &stringRes );
// ret is true (since call succeeded), stringRes.toString() is a string - "the result"
QVariant boolResult;
QString someString( "not important" );
args << someString;
// calls testClass->boolMethod( someString ), returning result in boolResult
ret = QCA::invokeMethodWithVariants( testClass1, QByteArray( "boolMethod" ), args, &boolResult );
// ret is true (since call succeeded), boolResult.toBool() is true.
Parameters
objthe object to call the method on
methodthe name of the method (without the arguments or brackets)
argsthe list of arguments to use in the method call
retthe return value of the method (unchanged if the call fails)
typethe type of connection to use
Returns
true if the call succeeded, otherwise false
QCA_EXPORT int methodReturnType ( const QMetaObject obj,
const QByteArray method,
const QList< QByteArray > &  argTypes 
)
related

Convenience method to determine the return type of a method.

This function identifies the return type of a specified method. This function can be used as shown:

class TestClass : public QObject
{
// ...
public slots:
QString qstringMethod() { return QString(); };
bool boolMethod( const QString & ) { return true; };
};
QByteArray myTypeName;
TestClass testClass;
QList<QByteArray> argsList; // empty list, since no args
myTypeName = QCA::methodReturnType( testClass.metaObject(), QByteArray( "qstringMethod" ), argsList );
// myTypeName is "QString"
myTypeName = QCA::methodReturnType( testClass.metaObject(), QByteArray( "boolMethod" ), argsList );
// myTypeName is "", because there is no method called "boolMethod" that has no arguments
argsList << "QString"; // now we have one argument
myTypeName = QCA::methodReturnType( testClass.metaObject(), QByteArray( "boolMethod" ), argsList );
// myTypeName is "bool"

The return type name of a method returning void is an empty string, not "void"

Note
This function is not normally required for use with QCA. It is provided for use in your code, if required.
Parameters
objthe QMetaObject for the object
methodthe name of the method (without the arguments or brackets)
argTypesthe list of argument types of the method
Returns
the name of the type that this method will return with the specified argument types.
See also
QMetaType for more information on the Qt meta type system.

The documentation for this class was generated from the following file:
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Oct 22 2021 23:04:25 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.