×
Namespaces

Variants
Actions

Qt Signal/Slot with custom data types

From Nokia Developer Wiki
Jump to: navigation, search

This article demonstrates how to pass custom data type via Signals and Slots.

Article Metadata
Tested with
Devices(s): Emulator / desktop / device
Compatibility
Platform(s): All Qt Supported
Symbian
Platform Security
Signing Required: Self-Signed
Capabilities: None
Article
Keywords: QObject
Created: skumar_rao (28 Nov 2010)
Last edited: hamishwillee (11 Oct 2012)

Connecting within a single thread

When connecting within a single thread, custom data types are used in the same way as any other types. Establish the signal/slot connection as usual:

connect(sender, SIGNAL(ownFunction(MyDataType&)), receiver, SLOT(ownFunction(MyDataType&)));

Connecting signals/slots between threads

But when passing custom data types across threads using SIGNAL & SLOT, we'll get a warning when attempting to establish a queued connection:

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

The solution is as the warning suggests. Call qRegisterMetaType() anywhere before establishing the connection:

qRegisterMetaType<MyDataType>("MyDataType");
connect(sender, SIGNAL(ownFunction(MyDataType&)), receiver, SLOT(ownFunction(MyDataType&)));

References

This page was last modified on 11 October 2012, at 04:18.
136 page views in the last 30 days.
×