S
S
Sergey Chistyakov2015-03-02 22:31:56
Qt
Sergey Chistyakov, 2015-03-02 22:31:56

Why discards qualifiers?

I am writing a model inherited from abstract QAbatractTableModel. Everything according to Feng Shui, methods, everything that needs to be rewritten. But I decided to cache the data in the cache. Therefore, you need to change the method dataand setData. As a result, it turned out:

QVariant MyModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) return QVariant();

    if (!cache.contains(m_course)) {
        QMap<QDate, Week> weeks;
        Week week;
        QVector<ScheduleItemPrintable> day;
        for (int nDay = 0; nDay < 7; ++nDay) {
            for (int nDoublePeriod = 0; nDoublePeriod < 6; ++nDoublePeriod)
                day << ScheduleItemPrintable();
            week << day;
        }
        weeks.insert(m_monday, week);
        cache.insert(m_course, weeks); // ошибка
    }
    //...
}

Model class interface:
class MyModel : public QAbstractTableModel
{
    Q_OBJECT
private:
    QStringList hheader;

    typedef QVector<QVector<ScheduleItemPrintable> > Week;
    QMap<QString, QMap<QDate, Week> > cache;

    QString m_course;
    QDate m_monday;
public:
    explicit MyModel(QObject *parent = 0);

    int rowCount(const QModelIndex &parent) const;
    int columnCount(const QModelIndex &parent) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role);
    QVariant data(const QModelIndex &index, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
};

In the marked place in the first listing, an error occurs:
error: passing 
'const QMap<QString, QMap<QDate, QVector<QVector<ScheduleItemPrintable> > > >'
as 'this' argument of 'QMap<Key, T>::iterator QMap<Key, T>::insert(const Key&, const T&) 
[with Key = QString; T = QMap<QDate, QVector<QVector<ScheduleItemPrintable> > >]' 
discards qualifiers [-fpermissive] cache.insert(m_course, weeks);

That's what I don't understand. Everything seems to be legal. The property lies in the object and there is access from the methods. What's wrong?
PS: the problem is solved with the pointer to the cache, but still I wonder why a simple object is not enough?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2015-03-02
@piro1107

If you need a cache in an object, you can mark it as mutable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question