Hi,

I have a very weird problem with a QMap of the type QMap<QString, QString>(). When I add this as a member to a class, I am not able to add anything to it, I always get the following compile error:

filemodel.cpp:32: error: passing 'const QMap<QString, QString>' as 'this' argument of 'QMap<Key, T>::iterator QMap<Key, T>::insert(const Key&, const T&) [with Key = QString; T = QString]' discards qualifiers [-fpermissive]
When I declare it a pointer member (e.g. QMap<QString, QString> *m_cache), it works.
I don't understand what is wrong, because I use this kind of map in other classes as well, without defining a pointer type.

The only difference is that this class is derived from a QFileSystemModel, but I can't believe that this is the cause for the compile error.

Here is my header file:
Qt Code:
  1. #include <QFileSystemModel>
  2. #include <QMap>
  3. #include <QString>
  4.  
  5. #include "modellerfile.h"
  6.  
  7. class ModellerFileModel : public QFileSystemModel
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit ModellerFileModel(QObject* const parent = 0);
  12.  
  13. int columnCount(const QModelIndex &parent=QModelIndex()) const;
  14. QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const;
  15. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  16.  
  17. QModelIndex setRootPath(const QString &path);
  18.  
  19. signals:
  20.  
  21. public slots:
  22.  
  23. private:
  24. QMap<QString, QString> m_cache;
  25. };
To copy to clipboard, switch view to plain text mode 

and this is the method throwing the compile error:
Qt Code:
  1. QVariant ModellerFileModel::data(const QModelIndex &index, int role) const
  2. {
  3. if (!index.isValid())
  4. {
  5. return QVariant();
  6. }
  7.  
  8. QFileInfo fi = fileInfo(index);
  9.  
  10. if (!m_cache.contains(fi.absoluteFilePath()))
  11. {
  12. ModellerFile* mfile = new ModellerFile(fi.absoluteFilePath());
  13. if (mfile->isValid())
  14. {
  15. m_cache.insert(fi.absoluteFilePath(), mfile->name());
  16. }
  17. }
  18.  
  19. [........]
  20.  
  21. }
To copy to clipboard, switch view to plain text mode 


The weird thing is, that if I create a map object inside the method for testing purposes, the insert() method of this temporary map works perfectly fine.
Why can't I define the member variable the way I do now, why does it only compile as a pointer type?

I don't understand the problem...