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

Cast QVariant to int

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.7k 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
    TomNow99
    wrote on last edited by TomNow99
    #1

    Hello,

    I have QVariant with int value on it:

    QVariant variant(3);
    

    Now I would like to do something like that:

    int value = variant;
    

    Of course I can't. I can't cast from QVariant to int. So I can use method toInt() from QVariant. But I would like to do that dynamically. I try:

    int value = ( variant.type() ) variant;
    

    Of course this doesn't work, but I would like something like that. Is there any way to do that?

    mrjjM JonBJ 2 Replies Last reply
    0
    • T TomNow99

      @JonB

      @mrjj Of course I know that

      int value = ( variant.type() ) variant;
      

      doesn't work and I know about "int" :)

      I have many QVariant variables and I would like to add them to QByteArray. But I can't add them to this Array like QVariant. I need to add them like int/char/double.

      So I would like do something like:

      QByteArray byte;
      QDataStream stream(&byte, QIODevice::WriteOnly);
      
      
      for( auto element: variants )
      {
          stream<<element; // here I have to cast QVariant to int/char/double
      }
      

      Of course I can do something like:

      if(metatype of element == metatype int )
      {
          stream<<element.toInt();
      }
      else if(metatype of element == metatype double )
      {
          stream<<element.toDouble();
      }
      

      But maybe is shorter solution?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #5

      @TomNow99
      But there isn't, for the same reason. QDataStream operator << needs to know the type at compile time, just the same question as before. Because we are C++ and that is a strongly typed language. Unless you're willing to stream it as a QVariant, which is not what you want.

      At the end of the day you can write a method just like you've shown just with the necessary ifs/switch for all the QVariant types you want to handle, which might be a dozen? Once you've written the method once, it's done.

      1 Reply Last reply
      2
      • T TomNow99

        Hello,

        I have QVariant with int value on it:

        QVariant variant(3);
        

        Now I would like to do something like that:

        int value = variant;
        

        Of course I can't. I can't cast from QVariant to int. So I can use method toInt() from QVariant. But I would like to do that dynamically. I try:

        int value = ( variant.type() ) variant;
        

        Of course this doesn't work, but I would like something like that. Is there any way to do that?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @TomNow99
        Hi
        Af far as i know - you cant really do that dynamic.

        Also if
        int value = ( variant.type() ) variant;
        did work, you would convert all to int which might not always be what you wanted.

        Can I ask what you need this for ?

        1 Reply Last reply
        2
        • T TomNow99

          Hello,

          I have QVariant with int value on it:

          QVariant variant(3);
          

          Now I would like to do something like that:

          int value = variant;
          

          Of course I can't. I can't cast from QVariant to int. So I can use method toInt() from QVariant. But I would like to do that dynamically. I try:

          int value = ( variant.type() ) variant;
          

          Of course this doesn't work, but I would like something like that. Is there any way to do that?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @TomNow99 said in Cast QVariant to int:

          int value = ( variant.type() ) variant;

          It seems to me the whole point of C++ typing is that you simply cannot do anything like this.....

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

            @JonB

            @mrjj Of course I know that

            int value = ( variant.type() ) variant;
            

            doesn't work and I know about "int" :)

            I have many QVariant variables and I would like to add them to QByteArray. But I can't add them to this Array like QVariant. I need to add them like int/char/double.

            So I would like do something like:

            QByteArray byte;
            QDataStream stream(&byte, QIODevice::WriteOnly);
            
            
            for( auto element: variants )
            {
                stream<<element; // here I have to cast QVariant to int/char/double
            }
            

            Of course I can do something like:

            if(metatype of element == metatype int )
            {
                stream<<element.toInt();
            }
            else if(metatype of element == metatype double )
            {
                stream<<element.toDouble();
            }
            

            But maybe is shorter solution?

            JonBJ 1 Reply Last reply
            0
            • T TomNow99

              @JonB

              @mrjj Of course I know that

              int value = ( variant.type() ) variant;
              

              doesn't work and I know about "int" :)

              I have many QVariant variables and I would like to add them to QByteArray. But I can't add them to this Array like QVariant. I need to add them like int/char/double.

              So I would like do something like:

              QByteArray byte;
              QDataStream stream(&byte, QIODevice::WriteOnly);
              
              
              for( auto element: variants )
              {
                  stream<<element; // here I have to cast QVariant to int/char/double
              }
              

              Of course I can do something like:

              if(metatype of element == metatype int )
              {
                  stream<<element.toInt();
              }
              else if(metatype of element == metatype double )
              {
                  stream<<element.toDouble();
              }
              

              But maybe is shorter solution?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #5

              @TomNow99
              But there isn't, for the same reason. QDataStream operator << needs to know the type at compile time, just the same question as before. Because we are C++ and that is a strongly typed language. Unless you're willing to stream it as a QVariant, which is not what you want.

              At the end of the day you can write a method just like you've shown just with the necessary ifs/switch for all the QVariant types you want to handle, which might be a dozen? Once you've written the method once, it's done.

              1 Reply Last reply
              2

              • Login

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