Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVariant backward conversion. How?

QVariant backward conversion. How?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 398 Views
  • 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.
  • B Offline
    B Offline
    bogong
    wrote on last edited by
    #1

    Hello all!
    I have QVariantList and I need to convert every element of this list to the pure Qt data type from QVariant automatically. Kind of backward convesion to original type from QVariant. Something like this:

    if (element.isInt()) {
       ...
    }
    if (element.isQString()) {
       ...
    }
    if (element.isAnyQVariant()) {
       ...
    }
    ...
    

    Not casting. Casting is statically defining type. I need dynamical function. I need kind of "isType" function. Is there any?

    ODБOïO 1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You can do if (element.type() == qMetaTypeId<SomeType>()) { ... }.

      B 1 Reply Last reply
      3
      • B bogong

        Hello all!
        I have QVariantList and I need to convert every element of this list to the pure Qt data type from QVariant automatically. Kind of backward convesion to original type from QVariant. Something like this:

        if (element.isInt()) {
           ...
        }
        if (element.isQString()) {
           ...
        }
        if (element.isAnyQVariant()) {
           ...
        }
        ...
        

        Not casting. Casting is statically defining type. I need dynamical function. I need kind of "isType" function. Is there any?

        ODБOïO Offline
        ODБOïO Offline
        ODБOï
        wrote on last edited by ODБOï
        #3

        edit sorry my bad @bogong you can can't use https://doc.qt.io/qt-5/qvariant.html#canConvert

        Chris KawaC 1 Reply Last reply
        0
        • ODБOïO ODБOï

          edit sorry my bad @bogong you can can't use https://doc.qt.io/qt-5/qvariant.html#canConvert

          Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LeLev canConvert is not exactly useful for detecting type. It will, for example, happily return true for converting int to QString.

          ODБOïO 1 Reply Last reply
          2
          • Chris KawaC Chris Kawa

            @LeLev canConvert is not exactly useful for detecting type. It will, for example, happily return true for converting int to QString.

            ODБOïO Offline
            ODБOïO Offline
            ODБOï
            wrote on last edited by
            #5

            @Chris-Kawa got it , thx

            1 Reply Last reply
            0
            • Chris KawaC Chris Kawa

              You can do if (element.type() == qMetaTypeId<SomeType>()) { ... }.

              B Offline
              B Offline
              bogong
              wrote on last edited by
              #6

              @Chris-Kawa Thx a lot. Issue closed.

              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