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 as uint16_t type
Qt 6.11 is out! See what's new in the release blog

QVariant as uint16_t type

Scheduled Pinned Locked Moved Solved General and Desktop
qvariantqmetatypetype
6 Posts 2 Posters 5.0k 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.
  • M Offline
    M Offline
    maratk1n
    wrote on last edited by maratk1n
    #1

    Hello!
    How do I create a QVariant with a data type uint16_t?
    If I understand correctly, I need QMetaType::UShort (unsigned short like, the same thing uint16_t).
    But now I'm creating a QVariant this way

    QVariant(QVariant::Type::UInt) vect; //There is not what I need here.
    

    And how do I determine the size of the data stored in the QVariant?
    Is this the right way?

    QMetaType::sizeOf(vect.type());
    
    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Since QVariant doesn't have a QVariant::Type for uint16_t you need the other constructor that takes the QMetaType::Type (assuming unsigned short and uint16_t are the same thing on your platform):

      QVariant vect(QMetaType::UShort, nullptr);
      

      Alternatively you can construct the variant from a value:

      QVariant vect = QVariant::fromValue((uint16_t)0);
      
      M 1 Reply Last reply
      4
      • Chris KawaC Chris Kawa

        Since QVariant doesn't have a QVariant::Type for uint16_t you need the other constructor that takes the QMetaType::Type (assuming unsigned short and uint16_t are the same thing on your platform):

        QVariant vect(QMetaType::UShort, nullptr);
        

        Alternatively you can construct the variant from a value:

        QVariant vect = QVariant::fromValue((uint16_t)0);
        
        M Offline
        M Offline
        maratk1n
        wrote on last edited by
        #3

        @Chris-Kawa thank you!
        Can you please suggest how to convert the QVariant into an array of bytes (uint8_t)?
        I found the perfect solution

        union {
                    float data;
                    uint8_t bytes[4];
                } r2b; //result to byte array
        

        But the data can be of any type (depending on the QVariant), accordingly, the size of the array can also be different.
        Is it possible to somehow create a variable of the same type as the QVariant value?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I found the perfect solution

          It's far from perfect, I assure you ;)

          Not a direct response, but I'm guessing your next question after that would be "how do I convert an array of bytes back into QVariant?" and then "how do I know which type of variant is encoded in an array of bytes?" and then "Why when I convert and save bytes on platform X and then read and convert back on platform Y the value is different?" and then "how do I know if the bytes I read in program version X are in the same format/version as the ones I wrote in program version Y?".

          All in all this is a topic of data serialization and Qt has specialized classes to deal with all this complexity. See QDataStream for details. It lets you serialize QVariant (provided that serialization is implemented for the types inside) to a QByteArray and back in a consistent, platform independent and versioned way.

          M 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            I found the perfect solution

            It's far from perfect, I assure you ;)

            Not a direct response, but I'm guessing your next question after that would be "how do I convert an array of bytes back into QVariant?" and then "how do I know which type of variant is encoded in an array of bytes?" and then "Why when I convert and save bytes on platform X and then read and convert back on platform Y the value is different?" and then "how do I know if the bytes I read in program version X are in the same format/version as the ones I wrote in program version Y?".

            All in all this is a topic of data serialization and Qt has specialized classes to deal with all this complexity. See QDataStream for details. It lets you serialize QVariant (provided that serialization is implemented for the types inside) to a QByteArray and back in a consistent, platform independent and versioned way.

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

            @Chris-Kawa said in QVariant as uint16_t type:

            but I'm guessing your next question

            No, I wanted to know just that. I do with templates

            template <class T>
                union res_to_byte{
                    T data;
                    uint8_t bytes[sizeof(T)];
                }; //result to byte array
            

            But I do not know what to put here

            res_to_byte</**here**/> r2b;
            
            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              The variant can hold data of different sizes. Templates are compile time so the size needs to be constant. The best you can do is assume a size of the biggest type you support. You can for example create a union of all supported types and get a sizeof of that. This is kinda what QVariant does inside so you'd be re-inventing a wheel a little, but that's one way to do it.

              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