title: Creating QList of multiple object types
body: <p>I need to make multiple instances of various class types (classA, classB, classC), and store then in a single list.  Is is possible to create these objects dynamically and then add them to a single QList?  Something like:</p>
  
  <pre><code>QList&lt;QObject&gt; mylist;
  mylist.append(classA());
  mylist.append(classB());
  mylist.append(classC());
  </code></pre>
  
  <p>This won't compile because the objects are not of type QObject.  I could static_cast them to QObject, but will I be able to typeid.name() to determine their origical types?  (To cast them back later)?</p>
  
  <p>I found an example <a href="https://qt-project.org/forums/viewthread/17106" rel="nofollow">here</a> of using a QList&lt; QVariant >, but the compiler complains that:</p>
  
  <blockquote>
    <p>error: no matching function for call to 'QVariant::QVariant(classA&amp;)'</p>
  </blockquote>
  
  <p>for this code:</p>
  
  <pre><code>QList&lt;QVariant&gt; mylist;
  mylist.append(QVariant(tempObj));
  </code></pre>
  
  <hr>
  
  <p>Update:</p>
  
  <p>I got it compiling by creating the object during the append operation, AND using ::fromValue.</p>
  
  <pre><code>mylist.append(QVariant::fromValue(ClassA(1,2,3)));
  </code></pre>
  
  <p>Not sure this is right.  1 - will this cause a memory leak/problem creating the object in the append statement?  2 - why would I need ::fromValue to make this work?  (Does this just make a second copy of the object?)</p>
  
  <p>I put a qDebug in my constructors, and I see that my constructor with parameters is called twice, then the copy constructor is called once, and the default (no parameters) constructor is never called.  I don't understand that...</p>