Qt/Qt Quick macro

Source: Internet
Author: User
Tags emit

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.

 

Friends who are new to Qt may feel mysterious and unfamiliar with the various macros that need to be declared during Qt usage. This article will introduce several macros frequently used in Qt: Q_OBJECT, SIGNAL and SLOT, Q_SIGNALS and Q_SLOTS, Q_EMIT, Q_INVOKABLE, Q_PROPERTY:

 

Macro header file source: $ QTDIR/src/corelib/kernel/qobjectdefs. h

Q_OBJECT

# Define Q_OBJECT/
Public :/
Q_OBJECT_CHECK/
Static const QMetaObject staticMetaObject ;/
Q_OBJECT_GETSTATICMETAOBJECT/
Virtual const QMetaObject * metaObject () const ;/
Virtual void * qt_metacast (const char *);/
QT_TR_FUNCTIONS/
Virtual int qt_metacall (QMetaObject: Call, int, void **);/

Macro Q_OBJECT is the most important among all Qt macros. Q_OBJECT is a service provided by the signal slot mechanism and all other metadata systems (inner province, invokeMethod, metadata property system, etc). For more information about Q_OBJECT, see QObject in Qt source code analysis.

SIGNAL and SLOT

These two macros are used to call the connect method:

QObject: connect (myButton, SIGNAL (clicked (), <br/> label, SLOT (showText ())); 

So what macro SIGNAL and SLOT did for us? Let's take a look at the source code:

$ QTDIR/src/corelib/kernel/qobjectdefs. h <br/> # define SLOT (a) qFlagLocation ("1" # a QLOCATION) <br/> # define SIGNAL (a) qFlagLocation ("2" # a QLOCATION) <br/> $ QTDIR/src/corelib/kernel/qobject. cpp <br/> const char * qFlagLocation (const char * method) <br/>{< br/> static int idx = 0; <br/> flagged_locations [idx] = method; <br/> idx = (idx + 1) % flagged_locations_count; <br/> return method; <br/>} 

Originally, it will return a string based on the defined SIGNAL and SLOT name. For example, SIGNAL (clicked () returns the string "2 clicked ()", SLOT (showText ()) returns the string "1 showText ()"

Q_SIGNALS and Q_SLOTS <br/> # define slots <br/> # define signals protected <br/> # define Q_SLOTS <br/> # define Q_SIGNALS protected 

Q_SIGNALS and Q_SLOTS are introduced by Qt 4.1. They are used to replace the keywords signals and slots, because they are better compatible with third-party signal slot mechanisms, such as boost libraries. Although Q_SIGNALS and Q_SLOTS seem to have nothing to do. Otherwise, the QT metadatabase compiler moc recognizes the macro Q_SIGNALS and Q_SLOTS declared in the header file. As the basis for generating metadata model data, see the code example shown at the end of this article.

Q_EMIT
#define Q_EMIT#define emit

Q_EMIT is used to replace the keyword emit, because it is better compatible with the third-party signal slot mechanism, such as the boost library.

Here, we can see that Q_EMIT looks the same simple, but they are different! The difference on the surface is that Q_SIGNALS and Q_SLOTS are used in header files, while Q_EMIT is used in the code line of sight. The essential difference is that Q_SIGNALS and Q_SLOTS will be recognized by moc and must be used. Q_EMIT or emit is dispensable. It will not be recognized by moc. The only reason for its existence is:Increase the readability of the Code.That is to say, the following code can work normally, but 2) the writing may anger your colleagues.

Void method () <br/>{< br/> 1) emit signalA (); <br/> 2) signalA (); <br/>} 

Q_INVOKABLE

# Define Q_INVOKABLE

 

The purpose of using Q_INVOKABLE to modify a member function is that the modified member function can be called by the metadata system. This mechanism is available inQt C ++/QML hybrid programming, Qt service framework,AndQt/HTML5 hybrid editingChengIs widely used. I will thenAnother articleIn-depth discussion.

 

 

Q_PROPERTY

 

# Define Q_PROPERTY (text)

 

Use Q_PROPERTY to declareAttributeThe property is similar to a member variable, but it can be accessed by the metadata system. This mechanism is used to implement the attributes of QML. The usage of Q_PROPERTY is as follows:

 

Q_PROPERTY(QString title READ title WRITE setTitle USER true)

 


 

Next, let's take a look at the use of the above macros and how the meta-object compiler uses these macros.

 

# Include <qdeclarativeitem> <br/> class ellipseitem: Public qdeclarativeitem <br/>{< br/> q_object <br/> q_property (qcolor read color write setcolor notify colorchanged) <br/> Public: <br/> ellipseitem (qdeclarativeitem * parent = 0); <br/> void paint (qpainter * painter, const qstyleoptiongraphicsitem * option, <br/> qwidget * widget = 0); <br/> const qcolor & color () const; <br/> void setcolor (const qcolor & newcolor ); <br/> q_invokable qcolor randomcolor () const; <br/> Public q_slots: <br/> void try1 (); <br/> void try2 () {}< br/> q_signals: <br/> void colorchanged (); <br/> void ready (); <br/> PRIVATE: <br/> qcolor m_color; <br/>}; <br/> 

 

The following code is automatically generated by the metabase compiler moc based on the preceding header file:

 

Static const uint qt_meta_data_ellipseitem [] ={< br/> // content: <br/> 5, // revision <br/> 0, // classname <br/> 0, 0, // classinfo <br/> 5, 14, // methods <br/> 1, 39, // properties <br/> 0, 0, // enums/sets <br/> 0, 0, // constructors <br/> 0, // flags <br/> 2, // signalcount <br/> // signals: Signature, parameters, type, Tag, flags <br/> 13, 12, 12, 12, 0x05, <br/> 28, 12, 12, 12, 0x05, <br/> // slots: Signature, parameters, type, Tag, flags <br/> 36, 12, 12, 12, 0x0a, <br/> 43, 12, 12, 12, 0x0a, <br/> // Methods: Signature, parameters, type, tag, flags <br/> 57, 12, 50, 12, 0x02, <br/> // properties: name, type, flags <br/> 71, 50, 0x43495103, <br/> // properties: yy_signal_id <br/> 0, <br/> 0 // EOD <br/> }; <br/> static const char qt_meta_stringdata_ellipseitem [] = {<br/> "ellipseitem/0/0 colorchanged ()/0 ready ()/0" <br/> "try1 () /0try2 ()/0 qcolor/0 randomcolor ()/0 "<br/>" color/0 "<br/> }; 

 

From the code example above, we can see that the meta-object compiler moc of QT recognizes the macro Q_SIGNALS, Q_SLOTS, Q_PROPERTY, and Q_PROPERTY declared in the header file. The metadata table is generated based on this. In this object data table, we can see that moc identifies:

  • Two signals: colorchanged (), ready (); (q_signals)
  • Two slots: try1 (), try2 () (Q_SLOTS)
  • Five methods, markedQ_INVOKABLEThe method randomColor () is recorded in the meta object String Array qt_meta_stringdata_EllipseItem.
  • One attribute: color (Q_PROPERTY)

 

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.