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 Queued Custom Type Example declares a Block class which is registered in the main.cpp file:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
...
qRegisterMetaType<Block>();
...
return app.exec();
}
This type is later used in a signal-slot connection in the window.cpp file:
Window::Window()
{
thread = new RenderThread();
...
connect(thread, SIGNAL(sendBlock(Block)), this, SLOT(addBlock(Block)));
...
setWindowTitle(tr("Queued Custom Type"));
}
If a type is used in a queued connection without being registered, a warning will be printed at the console; for example:
QObject::connect: Cannot queue arguments of type 'Block'
(Make sure 'Block' is registered using qRegisterMetaType().)
Making the Type Printable
It is often quite useful to make a custom type printable for debugging purposes, as in the following code:
Message message(body, headers);
qDebug() << "Original:" << message;
This is achieved by creating a streaming operator for the type, which is often defined in the header file for that type:
QDebug &operator<<(QDebug &dbg, const Message &message);
The implementation for the Message type in the Custom Type Example goes to some effort to make the printable representation as readable as possible:
QDebug &operator<<(QDebug &dbg, const Message &message)
{
QStringList pieces = message.body().split("\r\n", QString::SkipEmptyParts);
if (pieces.isEmpty())
dbg.nospace() << "Message()";
else if (pieces.size() == 1)
dbg.nospace() << "Message(" << pieces.first() << ")";
else
dbg.nospace() << "Message(" << pieces.first() << " ...)";
return dbg.maybeSpace();
}
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.
Further Reading
The Q_DECLARE_METATYPE() macro and qRegisterMetaType() function documentation contain more detailed information about their uses and limitations.
The Custom Type, Custom Type Sending and Queued Custom Type examples show how to implement a custom type with the features outlined in this document.
The Debugging Techniques document provides an overview of the debugging mechanisms discussed above.