Q_invokable and invokemethod

Source: Internet
Author: User

Respect Original Works and translations. For reprinting, please maintain the integrity of the article and provide the original author's address in the form of a hyperlink http://blog.csdn.net/changsheng230, so that you can easily ask and correct it.

 

In the article on Qt/Qt Quick macros, we will introduce several macros frequently used in Qt: Q_OBJECT, SIGNAL and SLOT, Q_SIGNALS and Q_SLOTS, Q_EMIT, Q_INVOKABLE, and Q_PROPERTY. Compared with other macros, Q_INVOKABLE is more mysterious, but the understanding and use of Q_INVOKABLE becomes more and more important. This article will discuss Q_INVOKABLE and the corresponding invokeMethod.

Q_INVOKABLE

# Define Q_INVOKABLE

Review the definition of Q_INVOKABLE, which is simply define in $ QTDIR/src/corelib/kernel/qobjectdefs. h to make moc recognize.

The purpose of using Q_INVOKABLE to modify a member function is that the modified member function can be called by the metadata system.

QMetaObject: invokeMethod

The static method QMetaObject: invokeMethod () is defined as follows:

    Bool QMetaObject: invokeMethod (QObject * obj, const char * member, Qt: ConnectionType type, <br/> QGenericReturnArgument ret, QGenericArgument val0 = QGenericArgument (0 ),...) 

     

InvokeMethod is used to call the method member of the object obj (note that member can be a signal or a slot). If member can be called, The system returns the truth; otherwise, the system returns false. QMetaObject: invokeMethod can be an asynchronous or synchronous call. It depends on the connection method Qt: ConnectionType type. If the type is Qt: DirectConnection, It is a synchronous call. If it is Qt: QueuedConnection, it is an asynchronous call. For example:

    QMetaObject: invokeMethod (object, "methodName", <br/> Qt: QueuedConnection, <br/> Q_ARG (type1, arg1), <br/> Q_ARG (type2, arg2 )); 

The preceding call is an asynchronous call. Note that the parameters shown above need to be hardcopied during build events, the class corresponding to the custom type of the parameter must provide a common constructor, destructor, and copy constructor. You must use the qregister1_ype () method provided by the Qt type registration system to register this custom type.

Q_INVOKABLE and QMetaObject: invokeMethod are all aroused by the metadata system. This mechanism is available inQt C ++/qml Mixed Programming,Cross-Thread Programming,Qt service framework and QT/HTML5 hybrid programmingAnd is widely used.

Qt C ++/QML Mixed Programming

The Qt object system is used to call the C ++ method in QML. Consider using the Qt C ++ defined method in QML, as shown in the following code:

Import QT 4.7 <br/> Import shapes 5.0 // custom module <br/> item {<br/> width: 300; height: 200 <br/> ellipse {<br/> X: 50; Y: 35; width: 200; Height: 100 <br/> color: "Blue" <br/> mousearea {<br/> anchors. fill: parent <br/> // call the randomcolor method defined in C ++ <br/> onclicked: parent. color = parent. randomcolor () <br/>}< br/>} 

To enable the above QML code to successfully call the randomColor () function defined in the following code, the most critical point is that the randomColor method is modified with Q_INVOKABLE.

# Include <qdeclarativeitem> <br/> class ellipseitem: Public qdeclarativeitem <br/>{< br/> q_object <br/> Public: <br/> q_invokable qcolor randomcolor () const; <br/>... <Br/>} 

For more details, refer to my another blog: QML and C ++ Mixed Programming

Use in cross-Thread Programming

How can we call the QObject method that stops in other threads? Qt provides a friendly and clean solution: to post an event to the event queue, event processing will focus on calling the methods we are interested in (of course, this requires the thread to have a running event loop ). The trigger mechanism is implemented by the introspection method provided by moc. Therefore, only signals, slots, andThe method marked as q_invokable can be called by other threads.. If you do not want to use the cross-thread signal and slot method to call the QObject method that stops in other threads. Another option is to declare the method as Q_INVOKABLE and useInvokemethodCall.

 

More details, translation event loop and thread

Qt Service Framework

The Qt service framework is released in Qt Mobility 1.0.2. A service is an independent component that provides operations defined by clients. The client can search for the service by the service name, version number, and interface provided by the service object. After finding the service, the framework starts the service and returns a pointer.

Services are implemented through plug-ins. To prevent the client from relying on a specific Library, the Service must inherit from QObject. In this way, the QMetaObject system can be used to provide dynamic discovery and wakeup services. To make the QmetaObject Mechanism fully work, the Service must satisfy all of its methods through signal, slot, property orInvokable methodAndQ_INVOKEBLETo achieve

The most common method for interacting with servicer is as follows:

QServiceManager manager;QObject *storage ;storage = manager.loadInterface("com.nokia.qt.examples.FileStorage"); if (storage)     QMetaObject::invokeMethod(storage, "deleteFile", Q_ARG(QString, "/tmp/readme.txt"));
The above Code uses the invokeMethod method provided by the service meta object to call the deleteFile () method of the file storage object. The client does not need to know the object type, so it is not linked to a specific service library. Of course, the deleteFile method on the server must be markedQ_INVOKEBLE,To be recognized by the metadata system.

One of the highlights of the Qt service framework is its supportCross-process communicationThe service can accept remote processes. After registration on the Service Manager, the process passes through signal, slot,Invokable methodCommunicate with property, just like a local object. The service can be shared between clients or a client.Note:Before the launch of the Qt service framework, the signal, slot, and invokable method only support cross-thread. It is a cross-Inbound service/customer segment communication (picture from the Nokia Forum ). Here we can clearly see that,Invokable methodAndQ_INVOKEBLEIt is an important tool for cross-city and cross-thread communication between objects.

 

For more information about Qt Service Framework and use cases, see the Qt Service Framework documentation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.