Qt文档翻译之-QObject

以下只是部分:


Detailed Description

The QObject class is the base class of all Qt objects.

QObject is the heart of the Qt Object Model. The central feature in this model is a very powerful mechanism for seamless object communication called signals and slots. You can connect a signal to a slot with connect() and destroy the connection withdisconnect(). To avoid never ending notification loops you can temporarily block signals with blockSignals(). The protected functions connectNotify() and disconnectNotify() make it possible to track connections.

QObjects organize themselves in object trees. When you create a QObject with another object as parent, the object will automatically add itself to the parent's children() list. The parent takes ownership of the object; i.e., it will automatically delete its children in its destructor. You can look for an object by name and optionally type using findChild() or findChildren().

Every object has an objectName() and its class name can be found via the corresponding metaObject() (see QMetaObject::className()). You can determine whether the object's class inherits another class in the QObject inheritance hierarchy by using the inherits() function.

When an object is deleted, it emits a destroyed() signal. You can catch this signal to avoid dangling references to QObjects.

QObjects can receive events through event() and filter the events of other objects. See installEventFilter() and eventFilter() for details. A convenience handler, childEvent(), can be reimplemented to catch child events.

Last but not least, QObject provides the basic timer support in Qt; see QTimer for high-level support for timers.

Notice that the Q_OBJECT macro is mandatory for any object that implements signals, slots or properties. You also need to run the Meta Object Compiler on the source file. We strongly recommend the use of this macro in all subclasses of QObjectregardless of whether or not they actually use signals, slots and properties, since failure to do so may lead certain functions to exhibit strange behavior.

All Qt widgets inherit QObject. The convenience function isWidgetType() returns whether an object is actually a widget. It is much faster than qobject_cast<QWidget *>(obj) or obj->inherits("QWidget").

Some QObject functions, e.g. children(), return a QObjectListQObjectList is a typedef for QList<QObject *>.

Thread Affinity

QObject instance is said to have a thread affinity, or that it lives in a certain thread. When a QObject receives a queued signal or a posted event, the slot or event handler will run in the thread that the object lives in.

Note: If a QObject has no thread affinity (that is, if thread() returns zero), or if it lives in a thread that has no running event loop, then it cannot receive queued signals or posted events.

By default, a QObject lives in the thread in which it is created. An object's thread affinity can be queried using thread() and changed using moveToThread().

All QObjects must live in the same thread as their parent. Consequently:

  • setParent() will fail if the two QObjects involved live in different threads.
  • When a QObject is moved to another thread, all its children will be automatically moved too.
  • moveToThread() will fail if the QObject has a parent.
  • If QObjects are created within QThread::run(), they cannot become children of the QThread object because the QThread does not live in the thread that calls QThread::run().

Note: QObject's member variables do not automatically become its children. The parent-child relationship must be set by either passing a pointer to the child's constructor, or by calling setParent(). Without this step, the object's member variables will remain in the old thread when moveToThread() is called.

No Copy Constructor or Assignment Operator

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.

The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass ofQObject as the value to be stored in one of the container classes. You must store pointers.

Auto-Connection

Qt's meta-object system provides a mechanism to automatically connect signals and slots between QObject subclasses and their children. As long as objects are defined with suitable object names, and slots follow a simple naming convention, this connection can be performed at run-time by the QMetaObject::connectSlotsByName() function.

uic generates code that invokes this function to enable auto-connection to be performed between widgets on forms created with Qt Designer. More information about using auto-connection with Qt Designer is given in the Using a Designer UI File in Your Application section of the Qt Designer manual.

Dynamic Properties

From Qt 4.2, dynamic properties can be added to and removed from QObject instances at run-time. Dynamic properties do not need to be declared at compile-time, yet they provide the same advantages as static properties and are manipulated using the same API - using property() to read them and setProperty() to write them.

From Qt 4.3, dynamic properties are supported by Qt Designer, and both standard Qt widgets and user-created forms can be given dynamic properties.

Internationalization (I18n)

All QObject subclasses support Qt's translation features, making it possible to translate an application's user interface into different languages.

To make user-visible text translatable, it must be wrapped in calls to the tr() function. This is explained in detail in the Writing Source Code for Translation document.


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

译文:

描述:

1、QObject类是所有Qt对象的基类
2、QObject是Qt对象模型的心脏。叫做“信号-槽”的无缝对象通讯机制是该对象模型的核心特性。
你可以通过Connect()方法来把一个信号连接到一个槽函数中,或通过disconnect()方法来摧毁这个连接。
为了避免无限循环的 通知循环,你可以通过blockSignals()方法来临时的阻塞该信号。
受保护的方法 connectNotify() 和 disconnectNotify() 使得追踪连接变得可能。
3、QObject在object tree中组织他们自己。当你用另一个对象作为parent来创建一个QObject时, 该对象将自动地把自己加入到 parent的 childern 列表中。
parent拥有该对象的所有权,例如,它将自动地在他的析构函数中销毁它的childrent。
你可以通过名字或使用 经findchild()方法或findchildrent()方法得到的optionally type来查找一个对象。

4、每一个对象都有一个 bojectName(),且它的类名可通过对应的metaObject()方法来找到。在QObject继承层次中,你可以通过使用 inherits() 方法来决定该对象类是否继承其他的类。

5、当一个对象被销毁时,他会发射一个destroyed()信号,你能够捕捉到该信号来避免悬挂对QObject对象的引用。

6、QObject可以通过event()方法来接收事件或过滤其他对象的事件。详细信息可查看  installEventFilter()方法和 eventFilter() 方法的说明。
    childEvent()方法,一个方便的处理器,可以通过重载该方法来捕获 “子事件”child events.

7、最后,但仍需注意的,在Qt中QObject提供了对基础的计时器timer的支持, 高阶的计数器支持可通过查看QTimer类的详细信息获得。

注意:对所有实现 "信号Signals","槽slots" 或 "特性properties"功能的对象来说,Q_OBJECT 宏都是强制性必须的, 同时你也必须在原文件上运行 "元对象编译器 Meta Object Compiler"。
不管是否使用了 "信号Signals","槽slots" 或 "特性properties"功能,我们强烈建议在所有的子类化QObject操作中,都使用 "Q_OBJECT 宏"。因为如果不这么做,很可能会导致某些功能表现出奇怪的行为来。

所有的 Qt Widgets 空间都继承自 QObject 类,  isWidgetType() 这个方便的方法将会返回一个对象是否真的是一个 widgets控件. 这个方法执行起来要比 qobject_cast<QWidget *>(obj) 方法或 obj->inherits("QWidget") 方法快得多。

有的QObject方法会返回 QObjectList, 比如说它的  children() 方法。 注:QObjectList 是  QList<QObject *> 定义的类型。

//************************************************************************************
Thread Affinity(线程关联)

QObject 实例据说拥有“线程关联”关系,或者说在某个确定的线程中生存。
当QObject 收到一个排队信号queued signal,或一个发布事件posted event,
槽函数或事件处理程序将会在该对象所在的线程中执行。

注意:如果一个QObject对象没有线程关联(这意味着Thread()方法返回0值),
或者说该对象所生存的线程没有执行事件循环,则该对象将不能接受“队列信号”queued signals或“发布事件” posted events。

在默认的情况下,QObject在创建它的线程中生存。一个对象的线程关联关系可以使用thread()方法来查询,或者使用MoveToThread()方法来改变。
所有的QObject对象必须和他们的父对象生存在同一个线程中,因此:
1、如果两个QObject对象生存在不同的线程中,那么在执行SetParent()时,将会执行失败。
2、当一个QObject对象被移动到了另一个线程,他的所有孩子也将被自动的移动过去。
3、当QObject已经有了父对象时,再执行MoveToThread()方法将会失败。
4、如果一个QObject中已经创建了 QThread::run() 方法,那么该对象将不能成为 QThread对象的孩子,因为QThread对象并不生存在调用 QThread::run()方法的线程中。

注意: QObject的成员变量不会自动成为它的孩子,“父子关系”必须通过传递 孩子的构造函数的指针 或 调用SetParent()方法来设置。
不执行这些步骤的话, 当调用MoveToThread()方法时,该对象的成员变量将仍然保留在原有的旧线程中。

//**********************************************************************
No Copy Constructor or Assignment Operator (没有拷贝构造函数或赋值运算符)

QObject既没有拷贝构造函数,也没有复制运算符。设计如此。 实际上,他们已经声明了,但却是在Q_DISABLE_COPY()宏标记的Private 段中,
事实上,所有直接或间接继承QObject的对象都使用了该 宏 ,来将拷贝构造函数和赋值运算符声明到Private段中。

所以,当你想使用QObject对象作为值时,将不得不使用指针。例如,由于没有拷贝构造函数,你将不能把QObject的子类或QObject对象当成值一样存储在容器类中,
这时,你不得不使用指针。


//*****************************************************
Auto-Connection(自动连接)
Qt的 “元-对象” 系统提供了在两个QObject子类或他们的孩子间,自动连接信号和槽函数的机制。只要对象定义了合适的对象名,以及它的槽函数遵循了简单的命名约束,
该连接将会在 run-time 中被  QMetaObject::connectSlotsByName() 方法执行。

uic生成代码调用该方法,以使得在被 Qt Designer创建的表格forms之上的widgets控件之间 的auto-connection 变得使能。

//**************************************************************************************************
Dynamic Properties(动态特性)

从Qt4.2开始, 在run-time中,QObject实例可被添加或移除“动态特性”。“动态特性”无需再编译时compile-time 中声明,但它仍然提供了像“静态特性”一样的好处,
并且可以使用相同的API函数来进行操作:可以使用 property()方法来读取他们,和用  setProperty()方法来写。

从Qt4.3开始, “动态特性”已被 Qt Designer 所支持,并且所有的标准Qt widgets控件 或 用户自定义forms都能被给予“动态特性”。


//************************************************************************************************
Internationalization (I18n) 国际化

所有的QObject 子类都支持 Qt的翻译特征, 这使得把一个应用的用户界面翻译成不同语言变得可能。
为使得 “用户可视( user-visible)” 文本变得可以翻译, 它必须被 tr()方法 包括进来,相关详细的解释在 《 Writing Source Code for Translation 》文档中。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值