From daa56f1b4e784d6497ae4fdeaa664f3a1a7b5143 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Wed, 11 Jun 2014 11:15:59 +0200 Subject: Suggest candidates when non-existent method passed to invokeMethod(). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QMetaObject::invokeMethod: No such method Object::someMethod(SomeType) becomes: QMetaObject::invokeMethod: No such method Object::someMethod(SomeType) Candidates are: someMethod(SomeOtherType) someMethod(YetAnotherType) Change-Id: I3566bca64423e2f8150d0d544fb4e35a5262b19e Reviewed-by: Jędrzej Nowacki Reviewed-by: Olivier Goffart --- src/corelib/kernel/qmetaobject.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/qmetaobject.cpp b/src/corelib/kernel/qmetaobject.cpp index accefb1ec4..cb63718680 100644 --- a/src/corelib/kernel/qmetaobject.cpp +++ b/src/corelib/kernel/qmetaobject.cpp @@ -1338,6 +1338,27 @@ QByteArray QMetaObject::normalizedSignature(const char *method) enum { MaximumParamCount = 11 }; // up to 10 arguments + 1 return value +/*! + Returns the signatures of all methods whose name matches \a nonExistentMember, + or an empty QByteArray if there are no matches. +*/ +static inline QByteArray findMethodCandidates(const QMetaObject *metaObject, const char *nonExistentMember) +{ + QByteArray candidateMessage; + // Prevent full string comparison in every iteration. + const QByteArray memberByteArray = nonExistentMember; + for (int i = 0; i < metaObject->methodCount(); ++i) { + const QMetaMethod method = metaObject->method(i); + if (method.name() == memberByteArray) + candidateMessage.append(" " + method.methodSignature() + '\n'); + } + if (!candidateMessage.isEmpty()) { + candidateMessage.prepend("\nCandidates are:\n"); + candidateMessage.chop(1); + } + return candidateMessage; +} + /*! Invokes the \a member (a signal or a slot name) on the object \a obj. Returns \c true if the member could be invoked. Returns \c false @@ -1455,8 +1476,9 @@ bool QMetaObject::invokeMethod(QObject *obj, } if (idx < 0 || idx >= meta->methodCount()) { - qWarning("QMetaObject::invokeMethod: No such method %s::%s", - meta->className(), sig.constData()); + // This method doesn't belong to us; print out a nice warning with candidates. + qWarning("QMetaObject::invokeMethod: No such method %s::%s%s", + meta->className(), sig.constData(), findMethodCandidates(meta, member).constData()); return false; } QMetaMethod method = meta->method(idx); -- cgit v1.2.3