|  | Home · Overviews · Examples |

# QObject Class Reference
[com.trolltech.qt.core module]

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

Inherited by `QAbstractEventDispatcher`, `QAbstractItemDelegate`, `QAbstractItemModel`, `QAbstractTextDocumentLayout`, `QAction`, `QActionGroup`, `QButtonGroup`, `QClipboard`, `QCompleter`, `QCoreApplication`, `QDataWidgetMapper`, `QDrag`, `QEventLoop`, `QFileSystemWatcher`, `QFtp`, `QGraphicsItemAnimation`, `QGraphicsScene`, `QGraphicsSvgItem`, `QGraphicsTextItem`, `QHttp`, `QInputContext`, `QIODevice`, `QItemSelectionModel`, `QLayout`, `QMimeData`, `QMovie`, `QSessionManager`, `QSettings`, `QShortcut`, `QSignalMapper`, `QSocketNotifier`, `QSound`, `QSqlDriver`, `QStyle`, `QSvgRenderer`, `QSyntaxHighlighter`, `QSystemTrayIcon`, `QTcpServer`, `QTextDocument`, `QTextObject`, `QTimeLine`, `QTimer`, `QTranslator`, `QUndoGroup`, `QUndoStack`, `QValidator`, and `QWidget`.

## 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 with disconnect(). 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.

Events are delivered in the thread in which the object was created; see Thread Support in Qt and `thread` for details. Note that event processing is not done at all for QObjects with no thread affinity (`thread` returns zero). Use the `moveToThread` function to change the thread affinity for an object and its children (the object cannot be moved if it has a parent).

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 `QObject` regardless 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 `QObjectList`. `QObjectList` is a typedef for QList<`QObject` *>.

### 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 Component 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.

See also `QMetaObject`, `QPointer`, `QObjectCleanupHandler`, and Object Trees and Object Ownership.