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. Qvariant_cast prevents general access to superclass-data
Forum Updated to NodeBB v4.3 + New Features

Qvariant_cast prevents general access to superclass-data

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 841 Views 1 Watching
  • 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.
  • J Offline
    J Offline
    juergenhuber
    wrote on last edited by
    #1

    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 data

    CSubclass 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

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Nierob
      wrote on last edited by
      #2

      You need to register a conversion function http://qt-project.org/doc/qt-5/qmetatype.html#registerConverter


      Nierob

      1 Reply Last reply
      0
      • J Offline
        J Offline
        juergenhuber
        wrote on last edited by
        #3

        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

        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