QListSpecialMethodsBase (QList and QMap) .contains results in error passing as ‘this’ argument discards qualifiers [-fpermissive]
-
I started porting to Qt6. The issue I'm having is with every time I use the "contains()" function on a map or list. Using this line as an example, the error I get is:
g++ -c -pipe -O2 -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_SQL_LIB -DQT_XML_LIB -DQT_CORE_LIB -I. -Isrc -I/opt/qt6.3-static/include -I/opt/qt6.3-static/include/QtWidgets -I/opt/qt6.3-static/include/QtGui -I/opt/qt6.3-static/include/QtNetwork -I/opt/qt6.3-static/include/QtSql -I/opt/qt6.3-static/include/QtXml -I/opt/qt6.3-static/include/QtCore -I. -I. -I/opt/qt6.3-static/mkspecs/linux-g++ -o workerassetdelete.o src/workerassetdelete.cpp In file included from /opt/qt6.3-static/include/QtCore/qvector.h:43, from /opt/qt6.3-static/include/QtCore/QVector:1, from src/dbmanager.h:25, from src/workerassetdelete.cpp:20: /opt/qt6.3-static/include/QtCore/qlist.h: In instantiation of ‘qsizetype QtPrivate::indexOf(const QList<T>&, const U&, qsizetype) [with V = Asset; U = Asset; qsizetype = long long int]’: /opt/qt6.3-static/include/QtCore/qlist.h:968:30: required from ‘qsizetype QListSpecialMethodsBase<T>::indexOf(const AT&, qsizetype) const [with AT = Asset; T = Asset; qsizetype = long long int]’ /opt/qt6.3-static/include/QtCore/qlist.h:80:31: required from ‘bool QListSpecialMethodsBase<T>::contains(const AT&) const [with AT = Asset; T = Asset]’ src/workerassetdelete.cpp:86:37: required from here /opt/qt6.3-static/include/QtCore/qlist.h:939:20: error: passing ‘const Asset’ as ‘this’ argument discards qualifiers [-fpermissive] 939 | if (*n == u) | ~~~^~~~ In file included from src/dbmanager.h:29, from src/workerassetdelete.cpp:20: src/asset.h:78:10: note: in call to ‘bool Asset::operator==(const Asset&)’ 78 | bool operator==(const Asset &right); | ^~~~~~~~ make: *** [Makefile:6555: workerassetdelete.o] Error 1
In searching through these forums, I found a lot of information about serialization, but I can't find any examples of how to call contains() correctly with Qt6.
Thanks! -
We can't really help without seeing your code which triggers this warning.
-
bool operator==(const Asset &right);
this function must be const as explained here:
/opt/qt6.3-static/include/QtCore/qlist.h:939:20: error: passing ‘const Asset’ as ‘this’ argument discards qualifiers [-fpermissive]
939 | if (*n == u)
| ~^~
In file included from src/dbmanager.h:29,
from src/workerassetdelete.cpp:20:
src/asset.h:78:10: note: in call to ‘bool Asset::operator==(const Asset&)’
78 | bool operator==(const Asset &right); -
@squinky86 said in QListSpecialMethodsBase (QList and QMap) .contains results in error passing as ‘this’ argument discards qualifiers [-fpermissive]:
this line as an example
you have anitpattern here
if (toDelete.keys().contains(a))
according to this https://www.kdab.com/uncovering-32-qt-best-practices-compile-time-clazy/ there should be simply
toDelete.contains(a)
Also, Q_FOREACH is rather deprecated and overall performance poor https://www.kdab.com/goodbye-q_foreach/
I'd fix those first and then see if the error persists.
-
@Christian-Ehrlicher I'm sorry - I clicked on the wrong menu - this is the correct answer and resolved the compile issue. I can't figure out how to go back and mark this as the answer.
-
Hehe, no problem :)