diff --git a/src/corelib/kernel/qobject.cpp b/src/corelib/kernel/qobject.cpp index c96fb446d6..617fadc1ab 100644 --- a/src/corelib/kernel/qobject.cpp +++ b/src/corelib/kernel/qobject.cpp @@ -3219,6 +3219,9 @@ bool QMetaObjectPrivate::disconnect(const QObject *sender, \snippet code/src_corelib_kernel_qobject.cpp 34 + If \a object itself has a properly set object name, its own signals are also + connected to its respective slots. + \sa QObject::setObjectName() */ void QMetaObject::connectSlotsByName(QObject *o) @@ -3227,7 +3230,10 @@ void QMetaObject::connectSlotsByName(QObject *o) return; const QMetaObject *mo = o->metaObject(); Q_ASSERT(mo); - const QObjectList list = o->findChildren(QString()); + const QObjectList list = // list of all objects to look for matching signals including... + o->findChildren(QString()) // all children of 'o'... + << o; // and the object 'o' itself + for (int i = 0; i < mo->methodCount(); ++i) { QByteArray slotSignature = mo->method(i).methodSignature(); const char *slot = slotSignature.constData(); diff --git a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp index 17752c405c..db21ab4fe4 100644 --- a/tests/auto/corelib/kernel/qobject/tst_qobject.cpp +++ b/tests/auto/corelib/kernel/qobject/tst_qobject.cpp @@ -406,6 +406,8 @@ public: connect(this, SIGNAL(on_Sender_signalLoopBack()), this, SLOT(slotLoopBack())); } + void emitSignalNoParams() { emit signalNoParams(); } + void emit_signal_with_underscore() { emit signal_with_underscore(); } public slots: void on_Sender_signalNoParams() { called_slots << 1; } @@ -419,6 +421,8 @@ public slots: void on_Sender_signalManyParams2(int i1, int i2, int i3, QString string, bool onoff) { called_slots << 7; Q_UNUSED(i1);Q_UNUSED(i2);Q_UNUSED(i3);Q_UNUSED(string);Q_UNUSED(onoff); } void slotLoopBack() { called_slots << 8; } + void on_Receiver_signalNoParams() { called_slots << 9; } + void on_Receiver_signal_with_underscore() { called_slots << 10; } protected slots: void o() { called_slots << -1; } @@ -426,11 +430,14 @@ protected slots: signals: void on_Sender_signalLoopBack(); + void signalNoParams(); + void signal_with_underscore(); }; void tst_QObject::connectSlotsByName() { AutoConnectReceiver receiver; + receiver.setObjectName("Receiver"); AutoConnectSender sender(&receiver); sender.setObjectName("Sender"); @@ -462,6 +469,14 @@ void tst_QObject::connectSlotsByName() receiver.called_slots.clear(); sender.emitSignalLoopBack(); QCOMPARE(receiver.called_slots, QList() << 8); + + receiver.called_slots.clear(); + receiver.emitSignalNoParams(); + QCOMPARE(receiver.called_slots, QList() << 9); + + receiver.called_slots.clear(); + receiver.emit_signal_with_underscore(); + QCOMPARE(receiver.called_slots, QList() << 10); } void tst_QObject::qobject_castTemplate()