This thread helped me a lot, thank you @dismine, but it didn't work for me in some cases, so I'm pasting my solution ( I don't want to edit the first answer )
I have enhanced it to get rid of the sed command, so it is cross-platform, it uses standard qmake's replace functions $$split(), $$join(), $$upper(), and $$take_first().
moduleList local variable is needed, it doesn't work without it.
Following works for Qt >=5.15.2 (it of course can work on lower versions, but I didn't test it):
# Mark all Qt headers as system headers
!win32-clang-msvc:!mingw:!unix: \
return()
win32-clang-msvc: \
optionToken = -imsvc
else: \
optionToken = -isystem
# I don't know why but on MSYS2 this line makes problem, it throws:
# fatal error: stdlib.h: No such file or directory
# On unix it is needed to avoid warnings from system headers
# No warnings on MSYS2 without it
!mingw: \
QMAKE_CXXFLAGS += $$optionToken $$shell_quote($$[QT_INSTALL_HEADERS])
for(module, QT) {
equals(module, "testlib"): \
QMAKE_CXXFLAGS += \
$$optionToken $$shell_quote($$[QT_INSTALL_HEADERS]/QtTest)
else {
# Capitalize a first letter, result: -isystem <path>/include/QtCore
moduleList = $$split(module, )
QMAKE_CXXFLAGS += \
$$optionToken $$shell_quote($$[QT_INSTALL_HEADERS]/Qt$$upper(\
$$take_first(moduleList))$$join(moduleList, ))
}
}
unset(optionToken)
unset(moduleList)
It dynamically generates correct -isystem or -imsvc compiler flags on the base of currently used Qt modules, so you can include this code in different projects/sub-projects.
I can recommend creating the system_headers.prf and enable it by the qmake CONFIG option CONFIG += system_headers.
Original system_headers.prf source code is from the TinyORM project.