Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Qt Signal/Slot with custom data types

From 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
Platform(s): All Qt Supported
Platform Security
Signing Required: Self-Signed
Capabilities: None
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:

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


This page was last modified on 11 October 2012, at 01:18.
163 page views in the last 30 days.