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 to QVector3D
Forum Updated to NodeBB v4.3 + New Features

QVariant to QVector3D

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 2.5k Views 2 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by
    #1

    Hello,
    is there a way to convert a QVariant to a QVector3D?

    Thanks!

    M 1 Reply Last reply
    1
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi,
      The following should work:

      QVector3D vec;
      variant.setData(QVariant::fromValue<QVector3D>(vec));
      vec=variant.value<QVector3D>();
      

      -Michael.

      beeckscheB 1 Reply Last reply
      2
      • m.sueM m.sue

        Hi,
        The following should work:

        QVector3D vec;
        variant.setData(QVariant::fromValue<QVector3D>(vec));
        vec=variant.value<QVector3D>();
        

        -Michael.

        beeckscheB Offline
        beeckscheB Offline
        beecksche
        wrote on last edited by beecksche
        #3

        @m.sue

        Thanks, i've also found:

        T qvariant_cast(const QVariant &value)
        
        1 Reply Last reply
        1
        • beeckscheB beecksche

          Hello,
          is there a way to convert a QVariant to a QVector3D?

          Thanks!

          M Offline
          M Offline
          MMD18
          wrote on last edited by
          #4

          @beecksche
          Hello, that's an old post. But in 2024 there's an implicit conversion operator for QVector3D into a QVariant, thus you could write it this way:
          05a03876-7ae2-4ca9-a778-df1189e552e7-image.png

          P.S. Antispam check got wild, sorry for using a pic.

          M 1 Reply Last reply
          0
          • M MMD18

            @beecksche
            Hello, that's an old post. But in 2024 there's an implicit conversion operator for QVector3D into a QVariant, thus you could write it this way:
            05a03876-7ae2-4ca9-a778-df1189e552e7-image.png

            P.S. Antispam check got wild, sorry for using a pic.

            M Offline
            M Offline
            MMD18
            wrote on last edited by
            #5

            @MMD18 Seems like qvariant_cast is the only solution when you don't know the type at compile time and don't want to write a template function only for this conversion.

            S 1 Reply Last reply
            0
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote on last edited by
              #6

              qvariant_cast is templated and is equivalent to QVariant::value.

              1 Reply Last reply
              0
              • M MMD18

                @MMD18 Seems like qvariant_cast is the only solution when you don't know the type at compile time and don't want to write a template function only for this conversion.

                S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                @MMD18 said in QVariant to QVector3D:

                Seems like qvariant_cast is the only solution when you don't know the type at compile time

                Is it actually possible to not provide the type to qvariant_cast? I don't see how the compiler would deduce the return type.

                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