Qt Documentation

Creating Custom Qt Types

Overview

When creating user interfaces with Qt, particularly those with specialized controls and features, developers sometimes need to create new data types that can be used alongside or in place of Qt's existing set of value types.

Standard types such as QSize, QColor and QString can all be stored in QVariant objects, used as the types of properties in QObject-based classes, and emitted in signal-slot communication.

In this document, we take a custom type and describe how to integrate it into Qt's object model so that it can be stored in the same way as standard Qt types. We then show how to register the custom type to allow it to be used in signals and slots connections.

Creating a Custom Type

Before we begin, we need to ensure that the custom type we are creating meets all the requirements imposed by QMetaType. In other words, it must provide:

The class also provides a constructor for normal use and two public member functions that are used to obtain the private data.

Declaring the Type with QMetaType

The Message class only needs a suitable implementation in order to be usable. However, Qt's type system will not be able to understand how to store, retrieve and serialize instances of this class without some assistance. For example, we will be unable to store Message values in QVariant.

The class in Qt responsible for custom types is QMetaType. To make the type known to this class, we invoke the Q_DECLARE_METATYPE() macro on the class in the header file where it is defined:

Q_DECLARE_METATYPE(Message);

This now makes it possible for Message values to be stored in QVariant objects and retrieved later. See the Custom Type Example for code that demonstrates this.

The Q_DECLARE_METATYPE() macro also makes it possible for these values to be used as arguments to signals, but only in direct signal-slot connections. To make the custom type generally usable with the signals and slots mechanism, we need to perform some extra work.

Creating and Destroying Custom Objects

Although the declaration in the previous section makes the type available for use in direct signal-slot connections, it cannot be used for queued signal-slot connections, such as those that are made between objects in different threads. This is because the meta-object system does not know how to handle creation and destruction of objects of the custom type at run-time.

To enable creation of objects at run-time, call the qRegisterMetaType() template function to register it with the meta-object system. This also makes the type available for queued signal-slot communication as long as you call it before you make the first connection that uses the type.

The output sent to the debug stream can, of course, be made as simple or as complicated as you like. Note that the value returned by this function is the QDebug object itself, though this is often obtained by calling the maybeSpace() member function of QDebug that pads out the stream with space characters to make it more readable.