Qt注册元类型

来源:互联网 发布:单片机通讯光耦隔离 编辑:程序博客网 时间:2024/05/01 14:08

Registering custom types

February 15th, 2009

Just a note here, if you would have to pass custom data types between threads in Qt. As we know, a signal-slot connection is then (by default) of type Qt::QueuedConnection. Because in such a situation Qt needs to store passed parameters for a while, it creates their temporary copies. If it doesn’t recognize the passed data type, throws out an error:

 

QObject::connect: Cannot queue arguments of type 'MyType'

 

So custom data types have to be registered using qRegisterMetaType(), like in the example:

 

qRegisterMetaType<MyType>( "MyType" );

 

And this example is literal ⇒ when your class is called MyType, you register it as "MyType". Lastly I did something similar to this:

 

  1  2  3
typedef QMap<QString,QImage> MapStringImage;(...)qRegisterMetaType<MapStringImage>( "images" );

 

I didn’t get the error from QObject::connect (!), but also didn’t get things working. Wasted few hours hacking QMetaType class with no effect, and then more by accident than design changed "images" to "MapStringImage" and woo-hoo! That was my only problem… That’s why I’m stressing this naming issue, especially that documentation doesn’t tell a lot about it.

BTW I needed to use typedef because otherwise Qt didn’t have a clue what to do with such a complex type.