Results 1 to 4 of 4

Thread: QMap only works when declared as a pointer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QMap only works when declared as a pointer

    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...

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMap only works when declared as a pointer

    The function is declared as const: ModellerFileModel::data(const QModelIndex &index, int role) const and it modifies state of the object (the map that is part of the object), to fix it you can drop const from function definition (because it's misleading) or add mutable to the QMap declaration (so that it can be modified in const member functions), the correct solution is the first one.

  3. #3
    Join Date
    Feb 2013
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QMap only works when declared as a pointer

    This seems to work, but then the original data() method of the QFileSystemModel doesn't seem to get overwritten. I will try the mutable keyword. Thanks for your quick answer!!!

    Andi

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QMap only works when declared as a pointer

    Yes, you have to keep the const (signatures must match) if you override that member function so you will have to use mutable or your first solution.

  5. The following user says thank you to Zlatomir for this useful post:

    TheGrudge (10th February 2013)

Similar Threads

  1. Where is the connection declared?
    By szisziszilvi in forum Newbie
    Replies: 7
    Last Post: 24th August 2011, 09:15
  2. Replies: 3
    Last Post: 6th July 2011, 06:59
  3. Replies: 1
    Last Post: 4th December 2010, 17:20
  4. [solved] Qmap and pointer issue
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 26th September 2007, 14:34
  5. Is there a Pointer Based QMap or Similar
    By vasudhamirji in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2006, 14:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.