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 * signalconst QObject * receiver, constchar * member )

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

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

    QLabel     *label  = new QLabel;
    QScrollBar *scroll = new QScrollBar;
    QObject::connect( scroll, SIGNAL(valueChanged(int)),
                      label,  SLOT(setNum(int)) );

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

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

    class MyWidget : public QWidget
    {
        Q_OBJECT
    public:
        MyWidget();

    signals:
        void myUsefulSignal();

    private:
        QPushButton *aButton;
    };

    MyWidget::MyWidget()
    {
        aButton = new QPushButton( this );
        connect( aButton, SIGNAL(clicked()), SIGNAL(myUsefulSignal()) );
    }

在这个实例中,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:

2、和connect 成双成对的disconnect

取消从sender对象发送的signal和对象receiver中的member的连接。

当这两个有关对象中的一个被销毁时,这个信号——槽连接就被移除了。

disconnect()通常被用在三种方式下,正如下面的例子所描述的。

1. 取消连接到一个对象的信号的任何事物:

disconnect( myObject, 0, 0, 0 );

等于非静态重载函数

myObject->disconnect();

2. 取消连接到一个特定信号的任何事物:

disconnect( myObject, SIGNAL(mySignal()), 0, 0 );

等于非静态重载函数

myObject->disconnect( SIGNAL(mySignal()) );

3. 取消一个特定接收者的所有连接:

disconnect( myObject, 0, myReceiver, 0 );

等于非静态重载函数

myObject->disconnect( myReceiver );

0可以用做一个通配符,意义可能分别为“任何信号”、“任何接受对象”或者“一个接受对象中的任何槽”。

sender不能为0。(你不能在一个调用中取消来自多于一个对象的信号的连接。)

如果signal为0,它取消所有和这个对象的信号连接的receiver和member。如果不是,只由特定信号被取消联接。

如果receiver为0,它取消连接到signal的任何事物。如果不是,只有receiver中的槽被取消连接。

如果member为0,它取消连接到receiver的任何事物。如果不是,只有名为member的槽被取消联接并且其它所有槽被保留。如果receiver被省去,member必须为0,所以你不能取消所有对象中同一特定名称槽的连接。

3、installEventFilter的使用

void QObject::installEventFilter ( const QObject * obj )

安装事件过滤器obj到这个对象。

事件过滤器就是接收所有被发送到这个对象的事件的对象。这个过滤器可以停止事件或者把它再转给这个对象。事件过滤器obj通过它的eventFilter()函数来接收事件。如果事件被过滤了(比如,停止了),eventFilter()函数必须返回真,否则它必须返回假。

如果有多个事件过滤器被安装到同一个对象上,最后一个被安装的事件过滤器将先被激活。

实例:


    #include <qwidget.h>

    class MyWidget : public QWidget
    {
        Q_OBJECT
    public:
        MyWidget( QWidget *parent = 0const char *name = 0 );

    protected:
        bool eventFilter( QObject *, QEvent * );
    };

    MyWidget::MyWidget( QWidget *parent, const char *name )
        : QWidget( parent, name )
    {
        // 为父对象(如果有的话)安装一个过滤器
        if ( parent )
            parent->installEventFilter( this );
    }

    bool MyWidget::eventFilter( QObject *o, QEvent *)
    {
        if ( e->type() == QEvent::KeyPress ) {
            // 对于键被按下进行特殊处理
            QKeyEvent *= (QKeyEvent *)e;
            qDebug( "Ate key press %d", k->key() );
            return TRUE; // 吃掉了这个事件
        } else {
            // 标准事件处理
            return QWidget::eventFilter( o, e );
        }
    }

例如,QAccel类使用这种技术来截取快捷键被按下。

警告:如果在你的eventFilter()函数中你删除了接收对象,请确认返回真。否则,Qt会把这个事件转给被删除的对象并且程序也许会崩溃。

4、tr国际化使用

QString QObject::tr ( const char * sourceText, const char * comment ) const

返回sourceText的翻译版本,或者如果没有合适的版本返回sourceText它自己。翻译上下文是comment的QObject(默认为空)。所有使用Q_OBJECT宏的QObject对象有这个函数的重新实现,把子类名作为上下文。

发布了1 篇原创文章 · 获赞 31 · 访问量 16万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章