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.

2 Responses to “Registering custom types”

comments feed Subscribe to comments
  1. Monah Tuk Says:

    Hi Ayoy, in additions, It good to register stream operators like qRegisterMetaTypeStreamOperators(“PhotoFormat”);

    And declare it with next prototypes: QDataStream &operator<<(QDataStream &out, const PhotoFormat &obj); QDataStream &operator>>(QDataStream &in, PhotoFormat &obj);

    It need for serialization class and usefull to use custom types with QSettings, for ex, implemenations of ‘operator<<’: QDataStream & operator <<(QDataStream &out, const PhotoFormat &obj) { QString name = obj.getFormatName(); QSizeF size = obj.getSize(); qreal dst1 = obj.getTopPateDistance(); qreal dst2 = obj.getNoseChinDistance(); }

    out << name << size << dst1 << dst2;
    return out;

    Code from real project :-)

  2. Monah Tuk Says:

    Ohhhh… Sorry my previous comment need formating…

Leave a Reply