Qt Cryptographic Architecture
|
#include <QtCrypto>
Public Member Functions | |
QVariant | call (QObject *obj, const QByteArray &method, const QVariantList &args=QVariantList(), bool *ok=0) |
void | start () |
void | stop () |
SyncThread (QObject *parent=0) | |
~SyncThread () | |
Protected Member Functions | |
virtual void | atEnd ()=0 |
virtual void | atStart ()=0 |
virtual void | run () |
Friends | |
class | Private |
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 QByteArray | methodReturnType (const QMetaObject *obj, const QByteArray &method, const QList< QByteArray > argTypes) |
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.
—
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:
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:
We can then use it like this:
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.
QCA::SyncThread::SyncThread | ( | QObject * | parent = 0 | ) |
Standard constructor.
parent | the parent object for this parent. |
QCA::SyncThread::~SyncThread | ( | ) |
void QCA::SyncThread::start | ( | ) |
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.
QVariant QCA::SyncThread::call | ( | QObject * | obj, |
const QByteArray & | method, | ||
const QVariantList & | args = QVariantList() , |
||
bool * | ok = 0 |
||
) |
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.
obj | the object to call the method on |
method | the name of the method (without the arguments or brackets) |
args | the list of arguments to use in the method call |
ok | if not 0, true is stored here if the call succeeds, otherwise false is stored here. |
|
protectedpure virtual |
Reimplement this to perform your initialization.
|
protectedpure virtual |
Reimplement this to perform your deinitialization.
|
protectedvirtual |
Starts the event loop and calls atStart and atStop as necessary.
|
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:
The return type name of a method returning void is an empty string, not "void"
obj | the QMetaObject for the object |
method | the name of the method (without the arguments or brackets) |
argTypes | the list of argument types of the method |
|
related |
Convenience method to invoke a method by name, using a variant list of arguments.
This function can be used as shown:
obj | the object to call the method on |
method | the name of the method (without the arguments or brackets) |
args | the list of arguments to use in the method call |
ret | the return value of the method (unchanged if the call fails) |
type | the type of connection to use |