HTML5   发布时间:2022-04-25  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了The Meta-Object System(Qt元对象系统)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Qt’s Meta-object system provides the signals and slots mechanism for inter-object communication,run-time type information,and the dynamic property system.

Qt元对象系统提供内建对象通信的信号和槽机制,运行时类信息,和动态属性系统。


The Meta-object system is based on three things:

元对象系统基于三样东西:


1.The QObject class provides a base class for objects that can take advantage of the Meta-object system.

1.QObject类是对象的基类,可以利用元对象系统的优势。


2.The Q_OBjeCT macro inside the private section of the class declaration is used to enable Meta-object features,such as dynamic properties,signals,and slots.

Q_OBjeCT宏在类的私有区声明,用来使用元对象的功能,例如:动态属性,信号和槽。

3.The Meta-Object Compiler (@H_997_64@moc) supplies each  subclass with the necessary code to implement Meta-object features.

元对象编译器(moc)为每个QObject子类提供必要的代码,使得其实现元对象的功能


The  tool reads a C++ source file. If it finds one or more class declarations that contain the  macro,it produces another C++ source file which contains the Meta-object code for each of those classes. This generated source file is either #include'd into the class's source file or,more usually,compiled and linked with the class's implementation


@H_647_10@moc工具读取C++源文件。如果它发现一个或多个包含Q_OBjeCT宏的类声明,它将为那些类产生一个包含了元对象代码的C++源文件生成文件包含d(#include'd,什么鬼)至类的源文件,还有,类的实现的编译和链接

In addition to providing the signals and slots mechanism for communication between objects (the main reason for introducing the system),the Meta-object code provides the following additional features:

除了为对象间提供信号和槽通信机制外(介绍系统的主要原因),元对象代码还提供以下额外功能


QObject::MetaObject() returns the associated Meta-object for the class.

QObject::MetaObject()返回类的相关联元对象。

QMetaObject::className() returns the class name as a String at run-time,without @R_874_10613@iring native run-time type information (RTTI) support through the C++ compiler.

QMetaObject::className()在运行时返回字符串类名,无需本地运行时类信息(RTTI)支持C++编译器。

QObject::inherits() function returns whether an object is an instance of a class that inherits a specified class within the  inheritance tree.

QObject::inherits()函数返回一个对象是否是在QObject继承树中指定类的派生类的实例。

QObject::tr() and QObject::trUtf8() translate Strings for internationalization.

QObject::tr()和QObject::trUtf8()翻译字符串以实现国际化。

QObject::setPropertyQObject::property() dynamically set and get properties by name.

QObject::setproperty()和QObject::property()通过名字动态地设置和获得属性

QMetaObject::newInstance() constructs a new instance of the class.

QMetaObject::newInstance()构造类的新实例。

it is also possible to perform dynamic casts using qobject_cast() on  classes. The () function behaves similarly to the standard C++ dynamic_cast(),with the advantages that it doesn't @R_874_10613@ire RTTI support and it works across dynamic library boundaries. It attempts to cast its argument to the pointer type specified in angle-brackets,returning a non-zero pointer if the object is of the correct type (determined at run-timE),or 0 if the object's type is incompatible.

另外也可以使用qobject_cast()执行QObject类之间的动态转换。qobject_cast()函数的行为类似标准C++的 dynamic_cast(),优势是它无需RTTI的支持并且可以跨动态库工作。它视图将参数转换成括号中指定类型的指针,如果对象是正确类型(在运行时确定的)返回非0指针,如果对象类型不符,返回0。

For example,let's assume @H_795_85@myWidget inherits from QWidget and is declared with the  macro:

例如,让我们假设MyWidget继承于QWidget并且使用Q_OBjeCT宏声明:

QObject *obj = new MyWidget;

obj variable,of type QObject *:rgb(49,actually refers to a  object,so we can cast it appropriately:

变量obj是QObject类型指针,实际上它指向MyWidget对象,所以我们可以适当转换它:

QWidget *widget = qobject_cast< *>(obj);

The cast from  to  is successful,because the object is actually a :rgb(49,which is a subclass of . Since we kNow that  is a :rgb(49,we can also cast it to @H_379_266@myWidget *:

从QObject到QWidget的转换是成功的,因为对象实际是MyWidget(QWidget的子类)。因为我们知道对象MyWidget,所以我们可以将它转换成MyWidget*:

@H_175_277@myWidget *myWidget = qobject_cast<MyWidget *>(obj);


The cast to  is successful because () makes no disTinction between built-in Qt types and custom types.

转换成MyWidget是成功的,因为qobject_cast()使内置的Qt类型和自定义类型之间没有区别。

QLabel *label = qobject_cast< *>(obj);

//label is 0

The cast to QLabel:rgb(49,on the other hand,fails. The pointer is then set to 0. This makes it possible to handle objects of different types differently at run-time,based on the type:

另外转换成QLabel失败,指针会被设置为0。这使得可能在运行时以不同的方式处理不同类型的对象,基于类型:

if(QLabel* label = qobject_cast<QLabel*>(obj))

{

label->setText(tr("Ping"));

}

else if(QPushButton* button = qobject_cast<QPushButton*>(obj))

button->setText(tr("Poing!"));

}

While it is possible to use  as a base class without the  macro and without Meta-object code,neither signals and slots nor the other features described here will be available if the  macro is not used. From the Meta-object system's point of view,a  subclass without Meta code is equivalent to its closest ancestor with Meta-object code. This means for example,that () will not return the actual name of your class,but the class name of this ancestor.

尽管可以使用无Q_OBjeCT声明和无元对象代码的QObject类作为基类,但如果不使用Q_OBjeCT宏信号和槽不可使用,这里描述的其他功能也不能使用。从元对象系统的角度看,一个QObject子类没有元代码就相当于它最近的有元对象代码的祖先。这就意味着,例如:QMetaObject::className()不会返回类的实际的名字而是祖先的类名。

Therefore,we strongly recommend that all subclasses of  use the  macro regardless of whether or not they actually use signals,slots,and properties.

所以我们强烈建议所有的QObject的子类中使用Q_OBjeCT宏而不管它实际是否使用信号,槽和属性

大佬总结

以上是大佬教程为你收集整理的The Meta-Object System(Qt元对象系统)全部内容,希望文章能够帮你解决The Meta-Object System(Qt元对象系统)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。