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

QVariant wrong cast

Scheduled Pinned Locked Moved Solved General and Desktop
qvariantcustom type
4 Posts 2 Posters 2.9k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    QVariant casting does not seem to be working right. See testcase below:

     #include <QApplication>
     #include <QVariant>
     
     class CustomClass1
     {
     public: 
       CustomClass1() = default;
       int a = 0;
     };
     
     Q_DECLARE_METATYPE(CustomClass1)
     
     template <typename T>
     QVariant variantify(T &&x)
     {
       QVariant variant;
       variant.setValue(std::forward<T>(x));
       return variant;
     }
     
     int main(int argc, char *argv[])
     {
       QApplication app(argc, argv);
     
       CustomClass1 cs;
       cs.a = 1000;
       QVariant const var = variantify(cs);
       if ( var.canConvert<CustomClass1>())
         {
           CustomClass1 const cs_cast = qvariant_cast<CustomClass1>(var);
           qDebug() << "val = " << cs_cast.a; // returns 0
         }
       else
         {
           qDebug() << "conversion failed";
         }
     
       return app.exec();
     }
    

    Expecting result: 1000.
    Returned result: 0 (default initialized value)

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

      Works for me. Which compiler and what version are you using?

      ? 2 Replies Last reply
      0
      • Chris KawaC Chris Kawa

        Works for me. Which compiler and what version are you using?

        ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @Chris-Kawa Tested with Qt 4.8.6, compiled with msvc2013

        1 Reply Last reply
        0
        • Chris KawaC Chris Kawa

          Works for me. Which compiler and what version are you using?

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @Chris-Kawa Please disregard. Sorry. I'm a complete idiot. I had the copy and assignment ctors decorated on my local version of testcase, and it was failing because of that.

          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