现在的位置: 首页 > 综合 > 正文

Qt Object 类简介--Qt 类简介专题(二) 第一页

2013年12月08日 ⁄ 综合 ⁄ 共 3564字 ⁄ 字号 评论关闭

给大家推荐一个学习Qt 和 Android 的网站(http://newfaction.net/ ),挺不错的。。gaga

 

Qt Object 类简介--Qt 类简介专题(二)

 

详细描述

QObject类是所有Qt对象的基类。

 

QObjectQt对象模型的中心。这个模型的中心特征就是一种用于无缝对象通讯的被叫做信号和槽的非常强大的机制。你能够使用connect()把信号和槽连接起来并且可以用disconnect()来破坏这种连接。为了避免从不结束的通知循环,你可以调用blockSignals()临时地阻塞信号。保护函数connectNotify()和disconnectNotify()使跟踪连接成为可能。

QObject把它们自己组织在对象树中。当你创建一个QObject作为其它对象的父对象,这个对象会在父对象中自动调用insertChild()并且可以在父对象的children()列表中显示出来。父对象拥有这个对象,比如,它将在它的析构函数中自动删除它的孩子。你可以使用child()或者queryList()通过名称和任意的类型来查找一个对象,并且使用objectTrees()来获得树根的列表。

 

每个对象都有一个对象名称(name()),能够报告它的类名(className())并且它在QObject继承层次中是否继承了另一个类(inherits())。

 

当对象被删除时,它发射destroyed()信号。你可以捕获这个信号来避免对QObject的摇摆引用。QGuardedPtr类提供了一种文雅的方式来使用这个机制。

QObject可以通过event()接收事件并且过滤其它对象的事件。详细情况请参考installEventFilter()和eventFilter()。一个方便的处理者,childEvent(),能够被重新实现来捕获子对象事件。

 

最后但不是最不重要的一点,QObject提供了Qt中最基本的定时器,关于定时器的高级支持请参考QTimer。

注意Q_OBJECT宏对于任何实现信号、槽和属性的对象都是强制的。你也需要对源文件运行moc程序(元对象编译器)。我们强烈建议在QObject的所有子类中使用这个宏,而不管它是不是实际使用了信号、槽和属性,因为不这样做也许会导致普通函数会出现为定义的问题。

 

所有的Qt窗口部件继承了QObject。方便的函数isWidgetType()返回这个对象实际上是不是一个窗口部件。它比inherits( “QWidget” )快得多。

 

一些QObject函数,比如children()、 objectTrees()和queryList()返回一个QObjectList。QObjectList是QObject的QPtrList。 QObjectLists支持像QPtrLists同样的操作并且又一个迭代器类QObjectListIt。也可以参考对象模型。

 

QObject 几个重要的成员函数

 

1、排第一位的当然是信号的绑定connect

bool QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * member )

[静态]
把从sender对象发送的signal和receiver对象中的member连接起来,并且如果连接成功返回真,否则返回假。

你必须在说明signal和member的时候使用SIGNAL()和SLOT()两个宏,例如:

这个实例确保了label总显示当前滚动条的值。

一个信号也可以被连接到另一个信号上:

在 这个实例中,MyWidget的构造函数传递一个来自私有成员变量的信号,并且使它在MyWidget的一个相关名称下可用。一个信号可以被连接到多个槽 和信号上。多个信号可以被连接到一个槽上。如果一个信号被连接到几个槽上,当信号被发射的时候,槽被激活的顺序是任意的。如果信号和槽被成功连接,返回 真。如果它不能创建连接,返回假,例如,如果QObject不能检验signal或member的存在,或者如果它们的标签不协调。也可以参考disconnect()。

有涉及多线程的需要注意它的第5个参数:
enum Qt::ConnectionType
This enum describes the types of connection that can be used between signals and slots. In particular, it determines whether a particular signal is delivered to a slot immediately or queued for delivery at a later time.

Constant

Value

Description

Qt::AutoConnection

0

(default) Same as DirectConnection, if the emitter and receiver are in the same thread. Same as QueuedConnection, if the emitter and receiver are in different threads.

Qt::DirectConnection

1

The slot is invoked immediately, when the signal is emitted.

Qt::QueuedConnection

2

The slot is invoked when control returns to the event loop of the receiver’s thread. The slot is executed in the receiver’s thread.

Qt::BlockingQueuedConnection

4

Same as QueuedConnection, except the current thread blocks until the slot returns. This connection type should only be used where the emitter and receiver are in different threads. Note: Violating this rule can cause your application to deadlock.

Qt::UniqueConnection

0x80

Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.

Qt::AutoCompatConnection

3

The default type when Qt 3 support is enabled. Same as AutoConnection but will also cause warnings to be output in certain situations. See Compatibility Signals and Slots for further information.

With queued connections, 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:

抱歉!评论已关闭.