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. Comparing QVariant::Type with custom registered types

Comparing QVariant::Type with custom registered types

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k 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.
  • S Offline
    S Offline
    sykac
    wrote on last edited by
    #1

    Hello,

    I'm trying to implement a conversion function which looks like this:

    CustomDataType conversionFunc(QVariant::Type type)
    {
        switch (type) {
        case QVariant::Int: return CustomDataType::C_INT;
        case QVariant::Double: return CustomDataType::C_DOUBLE;
        .
        .
        .
        default: return CustomDataType::C_INVALID;
        }
    }
    

    This works fine if I don't use non Qt data types in QVariant. I would like to add a new case e.g.:

    case QVariant::Eigen::Vector3d: return CustomDataType::C_VEC3D;
    

    This won't build although I registered the type with Q_DECLARE_METATYPE(Eigen::Vector3d). Is something like this possible? If so, how? I can still compare QVariant::typeName(), but isn't there some way of doing this using enums like in the code above?

    kshegunovK 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      https://doc.qt.io/qt-5/qvariant.html#userType

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      1
      • S sykac

        Hello,

        I'm trying to implement a conversion function which looks like this:

        CustomDataType conversionFunc(QVariant::Type type)
        {
            switch (type) {
            case QVariant::Int: return CustomDataType::C_INT;
            case QVariant::Double: return CustomDataType::C_DOUBLE;
            .
            .
            .
            default: return CustomDataType::C_INVALID;
            }
        }
        

        This works fine if I don't use non Qt data types in QVariant. I would like to add a new case e.g.:

        case QVariant::Eigen::Vector3d: return CustomDataType::C_VEC3D;
        

        This won't build although I registered the type with Q_DECLARE_METATYPE(Eigen::Vector3d). Is something like this possible? If so, how? I can still compare QVariant::typeName(), but isn't there some way of doing this using enums like in the code above?

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by kshegunov
        #3

        @sykac said in Comparing QVariant::Type with custom registered types:

        I registered the type with Q_DECLARE_METATYPE(Eigen::Vector3d).

        Why do you want to do that in the first place? What's the point of having an eigen vector in a QVariant?

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply
        2
        • S Offline
          S Offline
          sykac
          wrote on last edited by
          #4

          @kshegunov I'm using it in a generic interface - one method for passing multiple datatypes in one argument.

          @VRonin Thanks, that's what I needed.

          VRoninV 1 Reply Last reply
          0
          • S sykac

            @kshegunov I'm using it in a generic interface - one method for passing multiple datatypes in one argument.

            @VRonin Thanks, that's what I needed.

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @sykac said in Comparing QVariant::Type with custom registered types:

            one method for passing multiple datatypes in one argument

            Looks like you really want a template method instead

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            1

            • Login

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