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
Pick-to: 6.2 6.3
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
bb10
parent
3d9e56aa6c
commit
c613dd4765
|
|
@ -1525,7 +1525,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() \
|
||||
{ \
|
||||
|
|
|
|||
|
|
@ -8224,5 +8224,22 @@ void tst_QObject::emitToDestroyedClass()
|
|||
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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue