Results 1 to 2 of 2

Thread: Get unknown class from QVariant

  1. #1
    Join Date
    Jan 2012
    Dortmund, Germany
    Thanked 10 Times in 8 Posts
    Qt products
    Windows Android

    Question Get unknown class from QVariant

    I have many classes that inherit a common base class Entity.
    I have to iterate over many QVariants that each contain one object of one of those subclasses. I don't know the objects exact class, but Entity has a getClassname(), so that's fine. My problem is to safely get the subclass from the void* of QVariant::data() method.

    Qt Code:
    1. auto e = myVariant.value<QSharedPointer<Entity>>();
    To copy to clipboard, switch view to plain text mode 
    returns null

    Qt Code:
    1. auto ne = *reinterpret_cast<QSharedPointer<Entity>*>(;
    To copy to clipboard, switch view to plain text mode 
    works most of the time, but every now and then it doesn't - meaning that the object is broken (non-null!), crashing at once when using getClassname() or a bit later when using some other parts of the object.

    As I understand, I just cannot assume that using reinterpret_cast is safe because the internal representation of my Objects in QVariant might sometimes be "non-trivial".
    • Is that correct?
    • Is there any possibility to safely get my objects from the variant?

    Best regards,

  2. #2
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits

    Default Re: Get unknown class from QVariant

    QVariant stores a serialized copy of an object thus if the shared pointer gets invalidated I belive the stored instance might not be aware of it. I would be very hesitant to use a shared pointer with QVariant. The safest, foul proof approach I can suggest is to have a manager for Entity objects and store id of an object in QVariant so that when you read the value back, you can ask the manager for an object with the given id (if it exists).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.

  3. The following user says thank you to wysota for this useful post:

    sedi (21st April 2017)

Similar Threads

  1. Nice Solution to push Class on QVariant and take back...
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 6th October 2013, 15:58
  2. How to connect to a unknown class object signal?
    By wshn13 in forum Qt Programming
    Replies: 6
    Last Post: 20th March 2012, 07:56
  3. Replies: 7
    Last Post: 15th December 2009, 11:01
  4. Replies: 1
    Last Post: 16th April 2008, 00:15
  5. Model/View: Use own class with QVariant
    By No-Nonsense in forum Qt Programming
    Replies: 5
    Last Post: 18th December 2006, 18:21

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.