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. Can't get my object back from QVariant
QtWS25 Last Chance

Can't get my object back from QVariant

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 8.5k 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.
  • R Offline
    R Offline
    ronM71
    wrote on last edited by
    #1

    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
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      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
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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
        0
        • R Offline
          R Offline
          ronM71
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            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
            0
            • R Offline
              R Offline
              ronM71
              wrote on last edited by
              #6

              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
              0

              • Login

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