Qt Slot Call Order

Posted onby admin
Qt Slot Call Order Rating: 3,9/5 2614 votes

The following simple code snippet shows how to create and use QPushButton. It has been tested on Qt Symbian Simulator. An instance of QPushButton is created. Signal released is connected to slot handleButton which changes the text and the size of the button. To build and run the example: Create an empty folder. QSignalMapper inherits from QObject and provides a means of establishing a relationship between a set of zero-parameter signals and a one-parameter signal or slot. The call to setMapping inside the for loop establishes a mapping between a button and an integer value; for example, buttons3 is associated with the integer value 3. A developer can choose to connect to a signal by creating a function (a slot) and calling the connect function to relate the signal to the slot. Qt's signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. This can be extremely useful to debug an OpenGL problem. Note that if OpenGL rendering is happening in another thread, you must force the signal/slot connection type to Qt::DirectConnection in order to be able to see the actual backtrace. Refer to the LoggingMode enum documentation for more information about logging modes.

The QMetaMethod class provides meta-data about a member function. More...

Public Types

enum Access { Private, Protected, Public }
enum MethodType { Method, Signal, Slot, Constructor }

Public Functions

QMetaMethod::Access access() const
bool invoke(QObject *object, Qt::ConnectionType connectionType, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
bool invoke(QObject *object, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
bool invoke(QObject *object, Qt::ConnectionType connectionType, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
bool invoke(QObject *object, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
bool invokeOnGadget(void *gadget, QGenericReturnArgument returnValue, QGenericArgument val0 = QGenericArgument(nullptr), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
bool invokeOnGadget(void *gadget, QGenericArgument val0 = QGenericArgument(0), QGenericArgument val1 = QGenericArgument(), QGenericArgument val2 = QGenericArgument(), QGenericArgument val3 = QGenericArgument(), QGenericArgument val4 = QGenericArgument(), QGenericArgument val5 = QGenericArgument(), QGenericArgument val6 = QGenericArgument(), QGenericArgument val7 = QGenericArgument(), QGenericArgument val8 = QGenericArgument(), QGenericArgument val9 = QGenericArgument()) const
bool isValid() const
int methodIndex() const
QByteArray methodSignature() const
QMetaMethod::MethodType methodType() const
QByteArray name() const
int parameterCount() const
QList<QByteArray> parameterNames() const
int parameterType(int index) const
QList<QByteArray> parameterTypes() const
int returnType() const
int revision() const
const char *tag() const
const char *typeName() const

Static Public Members

Qt Slot Call Order
QMetaMethod fromSignal(PointerToMemberFunction signal)

Related Non-Members

bool operator!=(const QMetaMethod &m1, const QMetaMethod &m2)
bool operator(const QMetaMethod &m1, const QMetaMethod &m2)

Macros

Detailed Description

A QMetaMethod has a methodType(), a methodSignature(), a list of parameterTypes() and parameterNames(), a return typeName(), a tag(), and an access() specifier. You can use invoke() to invoke the method on an arbitrary QObject.

See also QMetaObject, QMetaEnum, QMetaProperty, and Qt's Property System.

Member Type Documentation

enum QMetaMethod::Access

This enum describes the access level of a method, following the conventions used in C++.

ConstantValue
QMetaMethod::Private0
QMetaMethod::Protected1
QMetaMethod::Public2

enum QMetaMethod::MethodType

ConstantValueDescription
QMetaMethod::Method0The function is a plain member function.
QMetaMethod::Signal1The function is a signal.
QMetaMethod::Slot2The function is a slot.
QMetaMethod::Constructor3The function is a constructor.

Member Function Documentation

Qt Slot Call Order

QMetaMethod::Access QMetaMethod::access() const

Returns the access specification of this method (private, protected, or public).

Note: Signals are always public, but you should regard that as an implementation detail. It is almost always a bad idea to emit a signal from outside its class.

See also methodType().

[static] template <typename PointerToMemberFunction> QMetaMethod QMetaMethod::fromSignal(PointerToMemberFunctionsignal)

Returns the meta-method that corresponds to the given signal, or an invalid QMetaMethod if signal is not a signal of the class.

Example:

This function was introduced in Qt 5.0.

bool QMetaMethod::invoke(QObject *object, Qt::ConnectionTypeconnectionType, QGenericReturnArgumentreturnValue, QGenericArgumentval0 = QGenericArgument(nullptr), QGenericArgumentval1 = QGenericArgument(), QGenericArgumentval2 = QGenericArgument(), QGenericArgumentval3 = QGenericArgument(), QGenericArgumentval4 = QGenericArgument(), QGenericArgumentval5 = QGenericArgument(), QGenericArgumentval6 = QGenericArgument(), QGenericArgumentval7 = QGenericArgument(), QGenericArgumentval8 = QGenericArgument(), QGenericArgumentval9 = QGenericArgument()) const

Invokes this method on the object object. Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

The invocation can be either synchronous or asynchronous, depending on the connectionType:

  • If connectionType is Qt::DirectConnection, the member will be invoked immediately.
  • If connectionType is Qt::QueuedConnection, a QEvent will be posted and the member is invoked as soon as the application enters the main event loop.
  • If connectionType is Qt::AutoConnection, the member is invoked synchronously if object lives in the same thread as the caller; otherwise it will invoke the member asynchronously.

The return value of this method call is placed in returnValue. If the invocation is asynchronous, the return value cannot be evaluated. You can pass up to ten arguments (val0, val1, val2, val3, val4, val5, val6, val7, val8, and val9) to this method call.

QGenericArgument and QGenericReturnArgument are internal helper classes. Because signals and slots can be dynamically invoked, you must enclose the arguments using the Q_ARG() and Q_RETURN_ARG() macros. Q_ARG() takes a type name and a const reference of that type; Q_RETURN_ARG() takes a type name and a non-const reference.

To asynchronously invoke the animateClick() slot on a QPushButton:

With asynchronous method invocations, the parameters must be of types that are known to Qt's meta-object system, because Qt needs to copy the arguments to store them in an event behind the scenes. If you try to use a queued connection and get the error message

call qRegisterMetaType() to register the data type before you call QMetaMethod::invoke().

To synchronously invoke the compute(QString, int, double) slot on some arbitrary object obj retrieve its return value:

QMetaObject::normalizedSignature() is used here to ensure that the format of the signature is what invoke() expects. E.g. extra whitespace is removed.

If the 'compute' slot does not take exactly one QString, one int and one double in the specified order, the call will fail.

Warning: this method will not test the validity of the arguments: object must be an instance of the class of the QMetaObject of which this QMetaMethod has been constructed with. The arguments must have the same type as the ones expected by the method, else, the behaviour is undefined.

See also Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), and QMetaObject::invokeMethod().

Qt Slot Call Ordering

bool QMetaMethod::invoke(QObject *object, QGenericReturnArgumentreturnValue, QGenericArgumentval0 = QGenericArgument(0), QGenericArgumentval1 = QGenericArgument(), QGenericArgumentval2 = QGenericArgument(), QGenericArgumentval3 = QGenericArgument(), QGenericArgumentval4 = QGenericArgument(), QGenericArgumentval5 = QGenericArgument(), QGenericArgumentval6 = QGenericArgument(), QGenericArgumentval7 = QGenericArgument(), QGenericArgumentval8 = QGenericArgument(), QGenericArgumentval9 = QGenericArgument()) const

This function overloads invoke().

This overload always invokes this method using the connection type Qt::AutoConnection.

bool QMetaMethod::invoke(QObject *object, Qt::ConnectionTypeconnectionType, QGenericArgumentval0 = QGenericArgument(0), QGenericArgumentval1 = QGenericArgument(), QGenericArgumentval2 = QGenericArgument(), QGenericArgumentval3 = QGenericArgument(), QGenericArgumentval4 = QGenericArgument(), QGenericArgumentval5 = QGenericArgument(), QGenericArgumentval6 = QGenericArgument(), QGenericArgumentval7 = QGenericArgument(), QGenericArgumentval8 = QGenericArgument(), QGenericArgumentval9 = QGenericArgument()) const

This function overloads invoke().

This overload can be used if the return value of the member is of no interest.

bool QMetaMethod::invoke(QObject *object, QGenericArgumentval0 = QGenericArgument(0), QGenericArgumentval1 = QGenericArgument(), QGenericArgumentval2 = QGenericArgument(), QGenericArgumentval3 = QGenericArgument(), QGenericArgumentval4 = QGenericArgument(), QGenericArgumentval5 = QGenericArgument(), QGenericArgumentval6 = QGenericArgument(), QGenericArgumentval7 = QGenericArgument(), QGenericArgumentval8 = QGenericArgument(), QGenericArgumentval9 = QGenericArgument()) const

This function overloads invoke().

This overload invokes this method using the connection type Qt::AutoConnection and ignores return values.

bool QMetaMethod::invokeOnGadget(void *gadget, QGenericReturnArgumentreturnValue, QGenericArgumentval0 = QGenericArgument(nullptr), QGenericArgumentval1 = QGenericArgument(), QGenericArgumentval2 = QGenericArgument(), QGenericArgumentval3 = QGenericArgument(), QGenericArgumentval4 = QGenericArgument(), QGenericArgumentval5 = QGenericArgument(), QGenericArgumentval6 = QGenericArgument(), QGenericArgumentval7 = QGenericArgument(), QGenericArgumentval8 = QGenericArgument(), QGenericArgumentval9 = QGenericArgument()) const

Invokes this method on a Q_GADGET. Returns true if the member could be invoked. Returns false if there is no such member or the parameters did not match.

The pointer gadget must point to an instance of the gadget class.

The invocation is always synchronous.

The return value of this method call is placed in returnValue. You can pass up to ten arguments (val0, val1, val2, val3, val4, val5, val6, val7, val8, and val9) to this method call.

Warning: this method will not test the validity of the arguments: gadget must be an instance of the class of the QMetaObject of which this QMetaMethod has been constructed with. The arguments must have the same type as the ones expected by the method, else, the behavior is undefined.

This function was introduced in Qt 5.5.

See also Q_ARG(), Q_RETURN_ARG(), qRegisterMetaType(), and QMetaObject::invokeMethod().

bool QMetaMethod::invokeOnGadget(void *gadget, QGenericArgumentval0 = QGenericArgument(0), QGenericArgumentval1 = QGenericArgument(), QGenericArgumentval2 = QGenericArgument(), QGenericArgumentval3 = QGenericArgument(), QGenericArgumentval4 = QGenericArgument(), QGenericArgumentval5 = QGenericArgument(), QGenericArgumentval6 = QGenericArgument(), QGenericArgumentval7 = QGenericArgument(), QGenericArgumentval8 = QGenericArgument(), QGenericArgumentval9 = QGenericArgument()) const

This is an overloaded function.

Qt slot call ordering

This overload invokes this method for a gadget and ignores return values.

This function was introduced in Qt 5.5.

bool QMetaMethod::isValid() const

Returns true if this method is valid (can be introspected and invoked), otherwise returns false.

This function was introduced in Qt 5.0.

int QMetaMethod::methodIndex() const

Returns this method's index.

This function was introduced in Qt 4.6.

QByteArray QMetaMethod::methodSignature() const

Returns the signature of this method (e.g., setValue(double)).

This function was introduced in Qt 5.0.

See also parameterTypes() and parameterNames().

Slot

QMetaMethod::MethodType QMetaMethod::methodType() const

Returns the type of this method (signal, slot, or method).

See also access().

QByteArray QMetaMethod::name() const

Returns the name of this method.

This function was introduced in Qt 5.0.

See also methodSignature() and parameterCount().

int QMetaMethod::parameterCount() const

Returns the number of parameters of this method.

This function was introduced in Qt 5.0.

See also parameterType() and parameterNames().

QList<QByteArray> QMetaMethod::parameterNames() const

Returns a list of parameter names.

See also parameterTypes() and methodSignature().

int QMetaMethod::parameterType(intindex) const

Returns the type of the parameter at the given index.

The return value is one of the types that are registered with QMetaType, or QMetaType::UnknownType if the type is not registered.

This function was introduced in Qt 5.0.

See also parameterCount(), returnType(), and QMetaType.

QList<QByteArray> QMetaMethod::parameterTypes() const

Returns a list of parameter types.

See also parameterNames() and methodSignature().

int QMetaMethod::returnType() const

Returns the return type of this method.

The return value is one of the types that are registered with QMetaType, or QMetaType::UnknownType if the type is not registered.

This function was introduced in Qt 5.0.

See also parameterType(), QMetaType, and typeName().

int QMetaMethod::revision() const

Returns the method revision if one was specified by Q_REVISION, otherwise returns 0.

This function was introduced in Qt 5.1.

const char *QMetaMethod::tag() const

Returns the tag associated with this method.

Qt Signal Slot Call Order

Tags are special macros recognized by moc that make it possible to add extra information about a method.

Tag information can be added in the following way in the function declaration:

and the information can be accessed by using:

For the moment, moc will extract and record all tags, but it will not handle any of them specially. You can use the tags to annotate your methods differently, and treat them according to the specific needs of your application.

Note: Since Qt 5.0, moc expands preprocessor macros, so it is necessary to surround the definition with #ifndefQ_MOC_RUN, as shown in the example above. This was not required in Qt 4. The code as shown above works with Qt 4 too.

const char *QMetaMethod::typeName() const

Returns the return type name of this method.

See also returnType() and QMetaType::type().

Related Non-Members

booloperator!=(const QMetaMethod &m1, const QMetaMethod &m2)

This is an overloaded function.

Qt Slot Call Orders

Returns true if method m1 is not equal to method m2, otherwise returns false.

This function was introduced in Qt 5.0.

booloperator(const QMetaMethod &m1, const QMetaMethod &m2)

This is an overloaded function.

Returns true if method m1 is equal to method m2, otherwise returns false.

This function was introduced in Qt 5.0.

Macro Documentation

Q_METAMETHOD_INVOKE_MAX_ARGS

Equals maximum number of arguments available for execution of the method via QMetaMethod::invoke()

© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.