subclassing QAbstractTableModel

  • When I subscass what should I call in data() for other cases than Qt::DisplayRole

    class TSimpleTM: public QAbstractTableModel
        virtual QVariant data(const QModelIndex & index,
                                int role = Qt::DisplayRole) const override
            if(role != Qt::DisplayRole)
               // return QVariant();
                QAbstractTableModel::data(index, role);

    For this code I get the stranger linker errror:
    TLDBAdmin_Form.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual class QVariant __thiscall QAbstractItemModel::data(class QModelIndex const &,int)const " (_imp?data@QAbstractItemModel@@UBE?AVQVariant@@ABVQModelIndex@@H@Z) referenced in function "public: virtual class QVariant __thiscall TSimpleTM::data(class QModelIndex const &,int)const " (?data@TSimpleTM@@UBE?AVQVariant@@ABVQModelIndex@@H@Z)

    Must I fill all other roles other than Display too or can I give in all other cases the invalid QVariant to return?
    I yes, I cannot really understand how the defaults work for the other rules.

  • @Andy314 Hi!
    No, you don't have to fill all other roles. Just write

    return QVariant() ;

    at the end of your reimplemented data() function.
    As I understand It works this way. Model inform View each time when data updated. View call data() function for each item, that were updated, if it get data for specific role, it displays it in a proper way, if it gets QVariant() it just doesn't display anything, or display it in default way. For example each item in QTableView may have small icon. But if you don't implement data() for Qt::DecorationRole view just will not show it. On the other hand if you don't implement data() for Qt::FontRole, view will display data with default font.

    As for linker error, have no ideas. Tried your code, and didn't get any linker error. Just try to clean project, it is the first thing I do when I get ANY linker error : )

Log in to reply

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