Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Closed] How to Convert a QVariant to a QList<MyTypedef>

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

Scheduled Pinned Locked Moved QML and Qt Quick
4 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheIlliterate
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      guziemic
      wrote on last edited by
      #2

      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
      @qRegisterMetaType(OwnType)@

      BR

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        [quote author="guziemic" date="1343971826"]
        If you plan to use your type in signals and slots then you should also call
        @qRegisterMetaType(OwnType)@
        [/quote]

        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.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TheIlliterate
          wrote on last edited by
          #4

          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

          Thanks
          TheIlliterate

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved