[Qt-interest] Q_DECLARE_METATYPE, qRegisterType, and threads...

Sean Harmer sean.harmer at maps-technology.com
Thu Nov 18 09:58:22 CET 2010


Hi,

On Wednesday 17 November 2010 21:35:54 BRM wrote:
> I recently wrote about changing my threading model from using a QThread
> derived class to just using straight QThreads and moving objects to them.
> Well, I now have a program where I do just that - for the worker thread it
> just creates the object and move it to a thread.
> However, I am now running into a problem with one of my custom types - an
> enum - that I use in a signal for logging information back to a central
> log - e.g. syslog.
> When the object in the new thread goes to emit the signal, I get the
> message that the enum is not registered:
> 
> QObject::connect: Cannot queue arguemtns for type 'yourEnum' (make sure
> 'yourEnum' is registered using qRegisterType().)
> 
> Problem is, I've declared it several ways that should make it valid to the
> meta-object system:
> 
> yourEnum {
> ...
> };
> Q_DECLARE_METATYPE(yourEnum)
> Q_ENUMS(yourEnum)
> ...
> 
> Note: The above is _not_ in a class definition but a shared header.
> 
> In other locations I have worked around this by making sure I register
> these custom types in the run() method - so it's registered in the thread
> on first use. (Otherwise it always complained about not being defined.)
> However, I don't have any kind of method here to do that - the first thing
> that gets called in the thread's object is something to handle data - it
> doesn't create/destroy anything in the new thread until then.
> 
> What am I doing wrong?

I am not entirely sure from your description above. Can you make a small 
compilable example that shows the problem please? You should just need to have 
a Q_DECLARE_METATYPE() call along with your enum declaration as you have shown 
here and then call qRegisterMetaType<yourEnum>( "yourEnum" ); before you try 
to use this type in a signal/slot connection that operates across threads.

Cheers,

Sean



More information about the Qt-interest-old mailing list