Let QMetaObject::connectSlotsByName(o) also check for signals of o

QMetaObject::connectSlotsByName(QObject* o) creates a list of all
children to look for signals that match slots of o. This changeset
simply adds the object o itself to that list.

The motivation is to finally fix the long standing QtCreator bug
QTCREATORBUG-6494. Where executing 'Go to slot...' and choosing
'accepted()' for a simple QDialog named 'MyDialog' will add a
on_MyDialog_accepted() slot to MyDialog. That slot never gets
connected. More details may be found in the linked QTBUG-7595.

Task-number: QTBUG-7595
Task-number: QTCREATORBUG-6494

Change-Id: I35f52761791af697eabb569adb5faee6fae50638
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
Axel Waggershauser 2013-03-06 17:18:48 +01:00 committed by The Qt Project
parent 079e3b4f3e
commit 6b68be9587
2 changed files with 22 additions and 1 deletions

View File

@ -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<QObject *>(QString());
const QObjectList list = // list of all objects to look for matching signals including...
o->findChildren<QObject *>(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();

View File

@ -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<int>() << 8);
receiver.called_slots.clear();
receiver.emitSignalNoParams();
QCOMPARE(receiver.called_slots, QList<int>() << 9);
receiver.called_slots.clear();
receiver.emit_signal_with_underscore();
QCOMPARE(receiver.called_slots, QList<int>() << 10);
}
void tst_QObject::qobject_castTemplate()