QMetaType: disable conversion from smart pointer<const QObject>

QMetaType can register a converter from a smart pointer class to
QObject *. The code tries to do so even if the smart pointer is
actually holding a pointer to a _const_ QObject
(e.g. shared_ptr<const QObject>), causing a compile error:

  ../src/qt5/qtbase/build/include/QtCore/../../../src/corelib/kernel/qmetatype.h:1208:32: error: invalid conversion from ‘const QObject*’ to ‘QObject*’ [-fpermissive]
   1208 |             return p.operator->();
        |                    ~~~~~~~~~~~~^~
        |                                |
        |                                const QObject*

Disable the conversion if indeed the source is const qualified.

Change-Id: I9e9bc5992f74131e5cfd6ece9b83d4f26d370e92
Fixes: QTBUG-103741
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit c613dd4765637a00837dd66abc2b940632022a60)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Giuseppe D'Angelo 2022-05-20 18:36:48 +02:00 committed by Qt Cherry-pick Bot
parent 8f75f04df9
commit 200a3c9297
2 changed files with 18 additions and 1 deletions

View File

@ -1520,7 +1520,7 @@ struct SharedPointerMetaTypeIdHelper<SMART_POINTER<T>, true> \
}; \
template<typename T> \
struct MetaTypeSmartPointerHelper<SMART_POINTER<T> , \
typename std::enable_if<IsPointerToTypeDerivedFromQObject<T*>::Value>::type> \
typename std::enable_if<IsPointerToTypeDerivedFromQObject<T*>::Value && !std::is_const_v<T>>::type> \
{ \
static bool registerConverter() \
{ \

View File

@ -8143,5 +8143,22 @@ void tst_QObject::objectNameBinding()
static_assert(QtPrivate::HasQ_OBJECT_Macro<tst_QObject>::Value);
static_assert(!QtPrivate::HasQ_OBJECT_Macro<SiblingDeleter>::Value);
Q_DECLARE_SMART_POINTER_METATYPE(std::shared_ptr)
Q_DECLARE_SMART_POINTER_METATYPE(std::unique_ptr)
// QTBUG-103741: OK to use smart pointers to const QObject in signals/slots
class SenderWithSharedPointerConstQObject : public QObject
{
Q_OBJECT
signals:
void aSignal1(const QSharedPointer<const QObject> &);
void aSignal2(const QWeakPointer<const QObject> &);
void aSignal3(const QPointer<const QObject> &);
void aSignal4(const std::shared_ptr<const QObject> &);
void aSignal5(const std::unique_ptr<const QObject> &);
};
QTEST_MAIN(tst_QObject)
#include "tst_qobject.moc"