Qvariant_cast prevents general access to superclass-data
-
Hey,
In a derived class from QAbstractTableModel I have impemented an user-role to give back the complete Object.
@
QVariant TmNotizTableModel::data(const QModelIndex &index, int role) const
{
int iRow,iCol, iMax;
CSubclass mySubclass;mySubclass = mySubclassList.at(index.row());
if( role == JUTABLEVIEW_USER_ROLE_OBJECT){
QVariant v;
qVariantSetValue(v, mySubclass);
return v;
}
}
@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.
@
bool JuTableView::edit (const QModelIndex & index, EditTrigger trigger, QEvent * event )
{
CBasic myCurrElement = qvariant_cast <CBasic> (index.model()->data(index, JUTABLEVIEW_USER_ROLE_OBJECT));
// myCurrElement is standard constructed and has no valid dataCSubclass myCurrSubclass = qvariant_cast <CSubclass> (index.model()->data(index, JUTABLEVIEW_USER_ROLE_OBJECT)); // Is valid including the elements inherited from CBasic.
}
@Here is a code snippet of the Class Architecture:
I have the following sample architecture:
@
class CBasic
{public:
CBasic();void init(void);
void setDbAdress(C_PKEY _dbadr){ dbAdress = _dbadr;};
C_PKEY getDbAdress(void){ return dbAdress;};QByteArray getDbStream(void){return myDbStream;};
void setDbStream(QByteArray _myDbStream){myDbStream = _myDbStream;};
void setDbStream(const char *data, int iSize){setDbStream(QByteArray(data, iSize));};int getUserId(void){return iUserId;};
void setUserId(int _iUserId){ iUserId = _iUserId;};protected:
C_PKEY dbAdress;
QByteArray myDbStream;
int iUserId;};
Q_DECLARE_METATYPE(CBasic);
class CSubclass : plublic CBasic
{public:
CSubclass();void init(void);
void setValue1(int _iValue1){ iValue1 = _iValue1;};
int getValue1(void){ return iValue1;};protected:
int iValue1;
};Q_DECLARE_METATYPE(CSubclass);
@Do you have any ideas? Best regards,
Jürgen -
You need to register a conversion function http://qt-project.org/doc/qt-5/qmetatype.html#registerConverter
-
Hey Nierob,
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.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 > 5.2.
Best regards,
Jürgen