Nested structs as signal arguments in Qt

    •     programming, tips'n'tricks

Worker objects in Qt are used to offload lengthy operations onto separate threads, out of the main event loop. They usually operate on data that already exists, e.g. building a palette from a QBitmap, or counting words in a QTextEdit.

However, there also exists another class of usages, where the worker object is tasked with producing some data, basically out of the ether. This happened to me just today: I wanted to pre-process a video to calculate its duration, resolution, and also average and maximum bit-rates. The most natural way to return that info would be to emit it inside a signal, so I wrote:

This compiled without a warning—and I’m pretty thorough with those, enabling -Wall and -Wextra, and turning them into errors with -Werror. So I went on to use it:

Again, it compiled—no warnings, no errors. But when I ran the program…

QObject::connect: Cannot queue arguments of type 'Result'
(Make sure 'Result' is registered using qRegisterMetaType().)
QObject::connect: Cannot queue arguments of type 'Result'
(Make sure 'Result' is registered using qRegisterMetaType().)

Uh-oh! What’s going on? Clearly I did register my type, it’s right there in Worker’s constructor…

As is usual with thorniest of Qt problems, this one is related to Moc—the program that implements signals and slots. It processes headers with Q_OBJECT in them, and generates additional files (like moc_worker.cpp) that implement Qt’s internals.

This particular issue is a limitation in Moc. QObject::connect compares types as strings, and it’s completely unaware of scopes. In my listings above, it doesn’t understand that Result in void finished(Result) and the Worker::Result throughout the rest of the code is one and the same type.

To fix this, fully qualify the type in signal’s definition:

Drop me a line! (wonder where’s the comments form?)