Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Can't get my object back from QVariant

    General and Desktop
    3
    6
    8157
    Loading More Posts
    • 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.
    • R
      ronM71 last edited by

      This is how encode my object (ContactPtr) as QVariant

      @QVariant ContactsModel::data(const QModelIndex &index, int role) const
      {
      if (!index.isValid())
      return QVariant();

      if (index.row() >= this->contacts.size())
      

      return QVariant();

      if (role == Qt::DisplayRole)
      {
      

      ContactPtr ptr = const_cast<Contact*>(&this->contacts[index.row()]);
      return QVariant(QVariant::UserType, &ptr);
      }
      else
      return QVariant();
      }@

      At some point in the rendering delegate, I am trying to get the ContactPtr back from the QVariant. I cannot get past the compilation errors for this. Everything I try fail. The ContactPtr is registered with:

      @typedef Contact * ContactPtr;
      Q_DECLARE_METATYPE(ContactPtr);@

      Can someone please help to point me in the right direction? I'd like to:

      // qv is my QVariant.
      @ContactPtr ptr = qv.value<ContactPtr>;@

      but it fails compilation. How do I do this??

      1 Reply Last reply Reply Quote 0
      • G
        giesbert last edited by

        youz are using the wrong functions, if you look at "the docs":http://doc.qt.nokia.com/4.7/qvariant.html#QVariant-3 . Use "qVariantFromValue":http://doc.qt.nokia.com/4.7/qvariant.html#qVariantFromValue instead.

        The point is with Q_DECLARE_METATYPE, you define an id for your type, which migth be QVariant::UserType, but also something else.

        To get it back, use "qVariantValue":http://doc.qt.nokia.com/4.7/qvariant.html#qVariantValue

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply Reply Quote 0
        • G
          goetz last edited by

          What type is ContactPtr? Just a

          @
          typedef Contact* ContactPtr
          @

          or something else?

          In the return line you put a pointer to the ptr (which is of type ContactPtr) object, i.e. you put a ContactPtr* into the QVariant. ptr obviously goes out of scope, once you leave the data() method, so you have a dangling pointer in your QVariant.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply Reply Quote 0
          • R
            ronM71 last edited by

            Ok. I've cancelled the ContactPtr. There was no real need for it. Now I do:

            @Q_DECLARE_METATYPE(Contact*);@

            I encode into QVariant like this:

            @return qVariantFromValue(const_cast<Contact*>(&this->contacts[index.row()]));@

            But I still cannot decode:

            @
            void ContactDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
            {
            painter->save();
            Contact * contact = qVariantValue(index.data()); // <---- error:
            @

            Error:
            ContactDelegate.cpp:15: error: no matching function for call to 'qVariantValue(QVariant)'

            1 Reply Last reply Reply Quote 0
            • G
              goetz last edited by

              qVariantValue is a template function. You must call:

              @
              Contact * contact = qVariantValue<Contact *>(index.data());
              @

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply Reply Quote 0
              • R
                ronM71 last edited by

                That did the job. I missed that little detail as the documentation's title is: "T qVariantValue ( const QVariant & value )"

                Thanks Volker and Gerolf

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post