0
votes

Why does the following code crash when I call idx.data()?

QVariant ApplicantTableModel::data(const QModelIndex &idx, int role) const
{
    if (!idx.isValid()) return QVariant();
    if (idx.column() == 10 && role == Qt::DisplayRole)
        if(idx.data() == "0")
            return "-";
        else return "+";
    else return QSqlTableModel::data(idx,role);
}
1
It crash because I call idx.data(), but I don't understend why? - user5636914

1 Answers

0
votes

If idx is an index of the same ApplicantTableModel instance (which it should be, otherwise the usage is incorrect) idx.data() will call idx.model()->data(), i.e. the very same ApplicantTableModel::data() function we’re looking at => infinite recursion, which leads to stack overflow/crash.

From your code I guess what you want might be

QVariant ApplicantTableModel::data(const QModelIndex &idx, int role) const
{
    if (!idx.isValid())
         return QVariant();
    if (idx.column() == 10 && role == Qt::DisplayRole) {
         if(QSqlTableModel::data(idx,role).toString() == “0")
             return "-";
         else
             return "+";
   }
   return QSqlTableModel::data(idx, role);
}

I.e. call get the value of data from the base class implementation and change that in this special case.