<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Qvariant_cast prevents general access to superclass-data]]></title><description><![CDATA[<p dir="auto">Hey,</p>
<p dir="auto">In a derived class from QAbstractTableModel I have impemented an user-role to give back the complete Object.</p>
<p dir="auto">@<br />
QVariant TmNotizTableModel::data(const QModelIndex &amp;index, int role) const<br />
{<br />
int         iRow,iCol, iMax;<br />
CSubclass  mySubclass;</p>
<pre><code>mySubclass = mySubclassList.at(index.row());
</code></pre>
<p dir="auto">if( role == JUTABLEVIEW_USER_ROLE_OBJECT){<br />
QVariant v;<br />
qVariantSetValue(v, mySubclass);<br />
return v;<br />
}<br />
}<br />
@</p>
<p dir="auto">In a derived class from QTableView I reimplemented the edit - Method to do some general access handling, which needs the CBaic-Data. The Problem is that qvariant_cast doesn't give me the valid superclass object back.</p>
<p dir="auto">@<br />
bool JuTableView::edit (const QModelIndex &amp; index, EditTrigger trigger, QEvent * event )<br />
{<br />
CBasic myCurrElement = qvariant_cast &lt;CBasic&gt; (index.model()-&gt;data(index, JUTABLEVIEW_USER_ROLE_OBJECT));<br />
// myCurrElement is standard constructed and has no valid data</p>
<pre><code>CSubclass myCurrSubclass = qvariant_cast &lt;CSubclass&gt; (index.model()-&gt;data(index, JUTABLEVIEW_USER_ROLE_OBJECT));
// Is valid including the elements inherited from CBasic. 
</code></pre>
<p dir="auto">}<br />
@</p>
<p dir="auto">Here is a code snippet of the Class Architecture:</p>
<p dir="auto">I have the following sample architecture:<br />
@<br />
class CBasic<br />
{</p>
<p dir="auto">public:<br />
CBasic();</p>
<p dir="auto">void   init(void);</p>
<p dir="auto">void      setDbAdress(C_PKEY _dbadr){ dbAdress = _dbadr;};<br />
C_PKEY  getDbAdress(void){ return dbAdress;};</p>
<p dir="auto">QByteArray  getDbStream(void){return myDbStream;};<br />
void   setDbStream(QByteArray _myDbStream){myDbStream = _myDbStream;};<br />
void   setDbStream(const char *data, int iSize){setDbStream(QByteArray(data, iSize));};</p>
<p dir="auto">int   getUserId(void){return iUserId;};<br />
void   setUserId(int _iUserId){ iUserId = _iUserId;};</p>
<pre><code>protected:
</code></pre>
<p dir="auto">C_PKEY    dbAdress;<br />
QByteArray   myDbStream;<br />
int    iUserId;</p>
<p dir="auto">};</p>
<p dir="auto">Q_DECLARE_METATYPE(CBasic);</p>
<p dir="auto">class CSubclass : plublic CBasic<br />
{</p>
<p dir="auto">public:<br />
CSubclass();</p>
<p dir="auto">void  init(void);</p>
<p dir="auto">void  setValue1(int _iValue1){ iValue1 = _iValue1;};<br />
int  getValue1(void){ return iValue1;};</p>
<pre><code>protected:
</code></pre>
<p dir="auto">int  iValue1;<br />
};</p>
<p dir="auto">Q_DECLARE_METATYPE(CSubclass);<br />
@</p>
<p dir="auto">Do you have any ideas? Best regards,<br />
Jürgen</p>
]]></description><link>https://forum.qt.io/topic/45991/qvariant_cast-prevents-general-access-to-superclass-data</link><generator>RSS for Node</generator><lastBuildDate>Mon, 14 Sep 2026 02:05:40 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/45991.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 16 Sep 2014 06:46:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qvariant_cast prevents general access to superclass-data on Tue, 16 Sep 2014 07:43:27 GMT]]></title><description><![CDATA[<p dir="auto">Hey Nierob,<br />
thank you very much for that link. Seems promising. Unfortunately I'm using 4.4.3 in our project. The linked functionality is introduced in 5.2 - it's not easy to do an upgrade at the moment due to other external library dependencies.</p>
<p dir="auto">Meanwhile I found a workaround: I implemented a further USER_ROLE to handle the Basic-Object. That works and I can upgrade to your better solution after the project upgrade to &gt; 5.2.</p>
<p dir="auto">Best regards,<br />
Jürgen</p>
]]></description><link>https://forum.qt.io/post/243971</link><guid isPermaLink="true">https://forum.qt.io/post/243971</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Tue, 16 Sep 2014 07:43:27 GMT</pubDate></item><item><title><![CDATA[Reply to Qvariant_cast prevents general access to superclass-data on Tue, 16 Sep 2014 07:23:57 GMT]]></title><description><![CDATA[<p dir="auto">You need to register a conversion function <a href="http://qt-project.org/doc/qt-5/qmetatype.html#registerConverter" target="_blank" rel="noopener noreferrer nofollow ugc">http://qt-project.org/doc/qt-5/qmetatype.html#registerConverter</a></p>
]]></description><link>https://forum.qt.io/post/243965</link><guid isPermaLink="true">https://forum.qt.io/post/243965</guid><dc:creator><![CDATA[Nierob]]></dc:creator><pubDate>Tue, 16 Sep 2014 07:23:57 GMT</pubDate></item></channel></rss>