From b66b145633e19a7f70aade0943e0c8efb5169a59 Mon Sep 17 00:00:00 2001 From: Thomas McGuire Date: Mon, 24 Sep 2012 12:25:49 +0200 Subject: Include the signal code in the argument of (dis)connectNotify(). As it turns out, the convention of (dis)connectNotify() is to include the signal prefix, i.e. '2'. Therefore add this prefix also when calling these functions from QML. Also add a unit test confirming that the C++ and QML cases are now handled the same way. This patch is not needed in Qt5, as connectNotify() and disconnectNotify() take a QMetaMethod as a parameter, not a const char*. Change-Id: I3add0fc13c60a479949cf3d31218af5fd3f546a2 Reviewed-by: Lars Knoll --- src/corelib/kernel/qobject.cpp | 14 ++----- src/corelib/kernel/qobject_p.h | 12 ++++++ src/declarative/qml/qdeclarativeboundsignal.cpp | 2 +- src/declarative/qml/qdeclarativenotifier_p.h | 2 +- src/declarative/qml/qdeclarativeproperty.cpp | 2 +- .../tst_qdeclarativenotifier.cpp | 47 +++++++++++++--------- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index a7c3b6e..bb5effc 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -2692,11 +2692,7 @@ bool QObject::connect(const QObject *sender, const QMetaMethod &signal, return false; } - // Reconstructing SIGNAL() macro result for signal.signature() string - QByteArray signalSignature; - signalSignature.reserve(qstrlen(signal.signature())+1); - signalSignature.append((char)(QSIGNAL_CODE + '0')); - signalSignature.append(signal.signature()); + QByteArray signalSignature = QObjectPrivate::signalSignature(signal); { QByteArray methodSignature; @@ -2985,13 +2981,9 @@ bool QObject::disconnect(const QObject *sender, const QMetaMethod &signal, } } - // Reconstructing SIGNAL() macro result for signal.signature() string QByteArray signalSignature; - if (signal.mobj) { - signalSignature.reserve(qstrlen(signal.signature())+1); - signalSignature.append((char)(QSIGNAL_CODE + '0')); - signalSignature.append(signal.signature()); - } + if (signal.mobj) + signalSignature = QObjectPrivate::signalSignature(signal); { QByteArray methodSignature; diff --git a/src/corelib/kernel/qobject_p.h b/src/corelib/kernel/qobject_p.h index c7aa683..e2e24a6 100644 --- a/src/corelib/kernel/qobject_p.h +++ b/src/corelib/kernel/qobject_p.h @@ -61,6 +61,7 @@ #include "QtCore/qvector.h" #include "QtCore/qreadwritelock.h" #include "QtCore/qvariant.h" +#include "QtCore/qmetaobject.h" QT_BEGIN_NAMESPACE @@ -184,6 +185,8 @@ public: inline void connectNotify(const char *signal); inline void disconnectNotify(const char *signal); + static inline QByteArray signalSignature(const QMetaMethod &signal); + public: QString objectName; ExtraData *extraData; // extra data set by the user @@ -244,6 +247,15 @@ inline void QObjectPrivate::disconnectNotify(const char *signal) q_ptr->disconnectNotify(signal); } +inline QByteArray QObjectPrivate::signalSignature(const QMetaMethod &signal) +{ + QByteArray result; + result.reserve(qstrlen(signal.signature())+1); + result.append((char)(QSIGNAL_CODE + '0')); + result.append(signal.signature()); + return result; +} + inline QObjectPrivate::Sender *QObjectPrivate::setCurrentSender(QObject *receiver, Sender *sender) { diff --git a/src/declarative/qml/qdeclarativeboundsignal.cpp b/src/declarative/qml/qdeclarativeboundsignal.cpp index c6ab1f1..878f31e 100644 --- a/src/declarative/qml/qdeclarativeboundsignal.cpp +++ b/src/declarative/qml/qdeclarativeboundsignal.cpp @@ -137,7 +137,7 @@ QDeclarativeBoundSignal::~QDeclarativeBoundSignal() void QDeclarativeBoundSignal::disconnect() { QObjectPrivate * const priv = QObjectPrivate::get(m_scope); - priv->disconnectNotify(m_signal.signature()); + priv->disconnectNotify(QObjectPrivate::signalSignature(m_signal)); } int QDeclarativeBoundSignal::index() const diff --git a/src/declarative/qml/qdeclarativenotifier_p.h b/src/declarative/qml/qdeclarativenotifier_p.h index ee80c9a..887d632 100644 --- a/src/declarative/qml/qdeclarativenotifier_p.h +++ b/src/declarative/qml/qdeclarativenotifier_p.h @@ -217,7 +217,7 @@ void QDeclarativeNotifierEndpoint::disconnect() QMetaObject::disconnectOne(s->source, s->sourceSignal, target, targetMethod); QObjectPrivate * const priv = QObjectPrivate::get(s->source); const QMetaMethod signal = s->source->metaObject()->method(s->sourceSignal); - priv->disconnectNotify(signal.signature()); + priv->disconnectNotify(QObjectPrivate::signalSignature(signal)); s->source = 0; } } else if (type == NotifierType) { diff --git a/src/declarative/qml/qdeclarativeproperty.cpp b/src/declarative/qml/qdeclarativeproperty.cpp index 2867d27..3c4c001 100644 --- a/src/declarative/qml/qdeclarativeproperty.cpp +++ b/src/declarative/qml/qdeclarativeproperty.cpp @@ -1635,7 +1635,7 @@ bool QDeclarativePropertyPrivate::connect(QObject *sender, int signal_index, const QMetaMethod signal = sender->metaObject()->method(signal_index); QObjectPrivate * const senderPriv = QObjectPrivate::get(sender); - senderPriv->connectNotify(signal.signature()); + senderPriv->connectNotify(QObjectPrivate::signalSignature(signal)); return QMetaObject::connect(sender, signal_index, receiver, method_index, type, types); } diff --git a/tests/auto/declarative/qdeclarativenotifier/tst_qdeclarativenotifier.cpp b/tests/auto/declarative/qdeclarativenotifier/tst_qdeclarativenotifier.cpp index 9fb028b..2cdd67f 100644 --- a/tests/auto/declarative/qdeclarativenotifier/tst_qdeclarativenotifier.cpp +++ b/tests/auto/declarative/qdeclarativenotifier/tst_qdeclarativenotifier.cpp @@ -105,30 +105,30 @@ protected: void connectNotify(const char *signal) { const QString signalName(signal); - if (signalName == "selfPropChanged()") selfPropConnections++; - if (signalName == "qmlObjectPropChanged()") qmlObjectPropConnections++; - if (signalName == "cppObjectPropChanged()") cppObjectPropConnections++; - if (signalName == "unboundPropChanged()") unboundPropConnections++; - if (signalName == "normalBindingPropChanged()") normalBindingPropConnections++; - if (signalName == "compiledBindingPropChanged()") compiledBindingPropConnections++; - if (signalName == "compiledBindingPropSharedChanged()") compiledBindingPropSharedConnections++; - if (signalName == "boundSignal()") boundSignalConnections++; - if (signalName == "unusedSignal()") unusedSignalConnections++; + if (signalName == SIGNAL(selfPropChanged())) selfPropConnections++; + if (signalName == SIGNAL(qmlObjectPropChanged())) qmlObjectPropConnections++; + if (signalName == SIGNAL(cppObjectPropChanged())) cppObjectPropConnections++; + if (signalName == SIGNAL(unboundPropChanged())) unboundPropConnections++; + if (signalName == SIGNAL(normalBindingPropChanged())) normalBindingPropConnections++; + if (signalName == SIGNAL(compiledBindingPropChanged())) compiledBindingPropConnections++; + if (signalName == SIGNAL(compiledBindingPropSharedChanged())) compiledBindingPropSharedConnections++; + if (signalName == SIGNAL(boundSignal())) boundSignalConnections++; + if (signalName == SIGNAL(unusedSignal())) unusedSignalConnections++; //qDebug() << Q_FUNC_INFO << this << signalName; } void disconnectNotify(const char *signal) { const QString signalName(signal); - if (signalName == "selfPropChanged()") selfPropConnections--; - if (signalName == "qmlObjectPropChanged()") qmlObjectPropConnections--; - if (signalName == "cppObjectPropChanged()") cppObjectPropConnections--; - if (signalName == "unboundPropChanged()") unboundPropConnections--; - if (signalName == "normalBindingPropChanged()") normalBindingPropConnections--; - if (signalName == "compiledBindingPropChanged()") compiledBindingPropConnections--; - if (signalName == "compiledBindingPropSharedChanged()") compiledBindingPropSharedConnections--; - if (signalName == "boundSignal()") boundSignalConnections--; - if (signalName == "unusedSignal()") unusedSignalConnections--; + if (signalName == SIGNAL(selfPropChanged())) selfPropConnections--; + if (signalName == SIGNAL(qmlObjectPropChanged())) qmlObjectPropConnections--; + if (signalName == SIGNAL(cppObjectPropChanged())) cppObjectPropConnections--; + if (signalName == SIGNAL(unboundPropChanged())) unboundPropConnections--; + if (signalName == SIGNAL(normalBindingPropChanged())) normalBindingPropConnections--; + if (signalName == SIGNAL(compiledBindingPropChanged())) compiledBindingPropConnections--; + if (signalName == SIGNAL(compiledBindingPropSharedChanged())) compiledBindingPropSharedConnections--; + if (signalName == SIGNAL(boundSignal())) boundSignalConnections--; + if (signalName == SIGNAL(unusedSignal())) unusedSignalConnections--; //qDebug() << Q_FUNC_INFO << this << signalName; } @@ -167,6 +167,8 @@ private slots: void propertyChange(); void disconnectOnDestroy(); + void nonQmlConnect(); + private: void createObjects(); @@ -296,6 +298,15 @@ void tst_qdeclarativenotifier::disconnectOnDestroy() QCOMPARE(exportedObject->cppObjectPropConnections, 0); } +void tst_qdeclarativenotifier::nonQmlConnect() +{ + ExportedClass a; + connect(&a, SIGNAL(boundSignal()), &a, SIGNAL(compiledBindingPropChanged())); + QCOMPARE(a.boundSignalConnections, 1); + disconnect(&a, SIGNAL(boundSignal()), &a, SIGNAL(compiledBindingPropChanged())); + QCOMPARE(a.boundSignalConnections, 0); +} + QTEST_MAIN(tst_qdeclarativenotifier) #include "tst_qdeclarativenotifier.moc" -- cgit v0.12