Qt World Summit: Register Today!

[Closed] How to Convert a QVariant to a QList<MyTypedef>

  • Hello

    I have a problem where I convert a QList<MyTypedef> object to a QVariant object and later on I need to convert the same QVariant object back to its QList<MyTypedef> type. so do I need to Q_DECLARE_METATYPE(MyTypedef) & Q_DECLARE_METATYPE( QList<MyTypedef> )?

    Also will there be an issue if we have multiple declaration of Q_DECLARE_METATYPE(MyTypedef) & Q_DECLARE_METATYPE( QList<MyTypedef> in dependent libraries?

  • Hi,

    As I know, the best place for calling Q_DECLARE_METATYPE is just after declaration of your own type. If you will call it several times there should be no issues. In addition, probably you do not need to declare metatype of QList as it is Qt object.
    If you plan to use your type in signals and slots then you should also call


  • [quote author="guziemic" date="1343971826"]
    If you plan to use your type in signals and slots then you should also call

    You also need that if you plan to use them in QSettings. In that case, or if you use them in queued signal slots connections, you also need to register the streaming operators for them. And because you can't know what others will do with your code in the future (you have no control over the type of connections that will be used to connect to the signals in your class in the future), you might as well do that directly.

  • Thanks Andre and guziemic :) for replying to the question That helped. What I found that I had to

    @Q_DECLARE_METATYPE( QList<MyTypeDef> )@

    otherwise compiler cries hoarse while converting to or from a QVariant object


Log in to reply