Qt注册元类型

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值