Convert Qt4.8 to Qt5.3 Project - QAbstractItemModel



  • Hi,

    I'm using sqlite database and generate some table models for qml. This is working fine with qt4.8.
    No i have converted the qt4 project to qt5.3 and the

    void setRoleNames(const QHash<int, QByteArray> & roleNames)
    

    is not longer available.

    How is the best way to use new "roleNames()" function?

    // current qt4.8
    void database_model::generate_role_names()
    {
        QHash<int, QByteArray> role_names;
        for(int i = 0; i < record().count(); ++i)
        {
            role_names[Qt::UserRole + i + 1] = record().fieldName(i).toLatin1(); // obsolete Qt5 .toAscii()
        }
        setRoleNames(role_names); // obsolete Qt5    
    }
    

    Any ideas?

    Thank you



  • Hi,
    the docu says you should use the virtual function
    QHash<int, QByteArray> QAbstractItemModel::roleNames() const
    instead.
    -Michael.



  • Hi,

    i tried this and it seems to work correct...

    QHash<int, QByteArray> database_model::roleNames() const
    {
        QHash<int, QByteArray> role_names;
        for(int i = 0; i < record().count(); ++i)
        {
            role_names[Qt::UserRole + i + 1] = record().fieldName(i).toLatin1();
        }
        return role_names;
    }
    
        protected:
            QHash<int, QByteArray> roleNames() const;
    

    Any ideas or comments or any better solution?

    Thanks
    Ferdl


  • Lifetime Qt Champion

    Hi,

    AFAIK, that's the correct way to do it.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.