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. How can I extract the underlying value from QVariant?
QtWS25 Last Chance

How can I extract the underlying value from QVariant?

Scheduled Pinned Locked Moved Unsolved General and Desktop
model view prog
3 Posts 3 Posters 724 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.
  • J Offline
    J Offline
    jdent
    wrote on 26 Jun 2024, 20:47 last edited by jdent
    #1

    I have a Model class of my own and need to extract the underlying value of the QVariant. That is, if the value in QVariant is a double I need to convert from QVariant to double, for instance.
    Do I need to do a switch with types? is QVariant::metaType the way to find the kind of value inside the QVariant? Can you show an example?

    C S 2 Replies Last reply 26 Jun 2024, 21:19
    0
    • J jdent
      26 Jun 2024, 20:47

      I have a Model class of my own and need to extract the underlying value of the QVariant. That is, if the value in QVariant is a double I need to convert from QVariant to double, for instance.
      Do I need to do a switch with types? is QVariant::metaType the way to find the kind of value inside the QVariant? Can you show an example?

      C Offline
      C Offline
      ChrisW67
      wrote on 26 Jun 2024, 21:19 last edited by
      #2

      @jdent For basic types there are obvious QVariant methods like QVariant::toDouble(). The QVariant documentation is quite useful.

      You can also register and handle custom types through QVariant. In that case you would use:

      Q_DECLARE_METATYPE(MyCustomType);
      MyCustomType foo;
      QVariant v = QVariant::fromValue(foo);
      MyCustomType bar = v.value<MyCustomType>();
      

      or similar functions depending on Qt version.

      1 Reply Last reply
      3
      • J jdent
        26 Jun 2024, 20:47

        I have a Model class of my own and need to extract the underlying value of the QVariant. That is, if the value in QVariant is a double I need to convert from QVariant to double, for instance.
        Do I need to do a switch with types? is QVariant::metaType the way to find the kind of value inside the QVariant? Can you show an example?

        S Offline
        S Offline
        SimonSchroeder
        wrote on 28 Jun 2024, 07:15 last edited by
        #3

        @jdent said in How can I extract the underlying value from QVariant?:

        Do I need to do a switch with types? is QVariant::metaType the way to find the kind of value inside the QVariant? Can you show an example?

        This would be one way to go. I have never had a need for this, but I'll give it a try how you could solve this with a switch statement:

        switch(myVariant.metaType().id())
        {
        case QMetaType::fromType<int>().id():
            // handle int
            break;
        case QMetaType::fromType<QString>().id():
            // handle QString
            break;
        default:
            // maybe a message/log that this case is unhandled
            break;
        }
        

        (I have never used QMetaType this extensively. I hope that calling id() is the smartest choice to distinguish these cases.)

        std::variant also has a solution with the overload pattern (using lambdas) using std::visit. It might be possible to use the overload pattern with std::visit on QVariants as well.

        1 Reply Last reply
        0

        1/3

        26 Jun 2024, 20:47

        • Login

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