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 is not working for user defined datatype

QVariant is not working for user defined datatype

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 712 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.
  • narinder83N Offline
    narinder83N Offline
    narinder83
    wrote on last edited by VRonin
    #1

    Hello There,

    I am facing difficulty with QVariant class. I've defined a sub class of QDateTime and trying to assign it to QVariant and read back. Q_DECLARE_METATYPE(myClass) and qRegisterMetaType declarations are in place still QVariant is not getting initialized with my defined class. Please suggest what is wrong here.

    #include <QCoreApplication>
    #include <QDateTime>
    #include <QVariant>
    #include <qdebug.h>
    
    class myClass:public QDateTime
    {
    
    
    };
    
    Q_DECLARE_METATYPE(myClass)
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        qRegisterMetaType<myClass>("myClass");
    
        myClass mc;
        mc.setDate (QDateTime::currentDateTime().date ());
        mc.setTime (QDateTime::currentDateTime() .time ());
    
        qDebug() << mc.date().day () << mc.date().month()  << mc.date().year () ;
    
        QVariant vc;
       vc.setValue (mc);
    
        myClass dateTime = vc.value<myClass>();
    
        qDebug() << dateTime.date().day () << dateTime.date().month()  << dateTime.date().year () ;
    
        return a.exec();
    }
    
    VRoninV 1 Reply Last reply
    0
    • narinder83N narinder83

      Hello There,

      I am facing difficulty with QVariant class. I've defined a sub class of QDateTime and trying to assign it to QVariant and read back. Q_DECLARE_METATYPE(myClass) and qRegisterMetaType declarations are in place still QVariant is not getting initialized with my defined class. Please suggest what is wrong here.

      #include <QCoreApplication>
      #include <QDateTime>
      #include <QVariant>
      #include <qdebug.h>
      
      class myClass:public QDateTime
      {
      
      
      };
      
      Q_DECLARE_METATYPE(myClass)
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          qRegisterMetaType<myClass>("myClass");
      
          myClass mc;
          mc.setDate (QDateTime::currentDateTime().date ());
          mc.setTime (QDateTime::currentDateTime() .time ());
      
          qDebug() << mc.date().day () << mc.date().month()  << mc.date().year () ;
      
          QVariant vc;
         vc.setValue (mc);
      
          myClass dateTime = vc.value<myClass>();
      
          qDebug() << dateTime.date().day () << dateTime.date().month()  << dateTime.date().year () ;
      
          return a.exec();
      }
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      @narinder83 said in QVariant is not working for user defined datatype:

      myClass dateTime = vc.value<myClass>();

      it might just be a problem of the default copy constructor. try defining it.

      class myClass : public QDateTime
      {
      public:
      myClass() = default;
      ~myClass() = default;
      myClass(myClass&& other)
      :QDateTime(static_cast<QDateTime&&>(other))
      {}
      myClass(const myClass& other)
      :QDateTime(static_cast<const QDateTime&>(other))
      {}
      myClass& operator=(const myClass& other){
      QDateTime::operator=(static_cast<const QDateTime&>(other));
      return *this;
      }
      myClass& operator=(myClass&& other){
      QDateTime::operator=(static_cast<QDateTime&&>(other));
      return *this;
      }
      };
      

      "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
      2

      • Login

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