aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4qobjectwrapper.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2022-03-25 10:56:41 +0100
committerUlf Hermann <ulf.hermann@qt.io>2022-03-28 10:28:35 +0200
commit3fd49c82cf4eccd24c5141d9dcb8a41722e70fc2 (patch)
tree7df63f092d4cc256c78597316371bb89eacbf9b3 /src/qml/jsruntime/qv4qobjectwrapper.cpp
parent664aa0c1893e03d4de44959f188f06082bcf5c0c (diff)
Respect invokable toString() methods
We should not invoke the base toString() method if there is an override in a more specific type. This involves lowering the priority of generic JS lookups when resolving scope properties, in favor of context and scope lookup. [ChangeLog][QtQml] You can now override the JavaScript toString() method by providing a Q_INVOKABLE method of the same name in your QObject-based C++ classes. Fixes: QTBUG-87697 Change-Id: I6190111f4c28e54ce76c391c69c4a921e290e612 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4qobjectwrapper.cpp')
-rw-r--r--src/qml/jsruntime/qv4qobjectwrapper.cpp33
1 files changed, 22 insertions, 11 deletions
diff --git a/src/qml/jsruntime/qv4qobjectwrapper.cpp b/src/qml/jsruntime/qv4qobjectwrapper.cpp
index 5d29cab79b..8f7f65027c 100644
--- a/src/qml/jsruntime/qv4qobjectwrapper.cpp
+++ b/src/qml/jsruntime/qv4qobjectwrapper.cpp
@@ -2211,24 +2211,35 @@ void Heap::QObjectMethod::ensureMethodsCache()
ReturnedValue QObjectMethod::method_toString(ExecutionEngine *engine) const
{
- QString result;
- if (const QMetaObject *metaObject = d()->metaObject()) {
+ const auto encode = [engine](const QString &result) {
+ return engine->newString(result)->asReturnedValue();
+ };
- result += QString::fromUtf8(metaObject->className()) +
- QLatin1String("(0x") + QString::number((quintptr)d()->object(),16);
+ if (const QMetaObject *metaObject = d()->metaObject()) {
+ if (QObject *qobject = d()->object()) {
+ const int id = metaObject->indexOfMethod("toString()");
+ if (id >= 0) {
+ const QMetaMethod method = metaObject->method(id);
+ const QMetaType returnType = method.returnMetaType();
+ QVariant result(returnType);
+ method.invoke(qobject, QGenericReturnArgument(returnType.name(), result.data()));
+ return engine->fromVariant(result);
+ }
- if (d()->object()) {
- QString objectName = d()->object()->objectName();
+ QString result;
+ result += QString::fromUtf8(metaObject->className()) +
+ QLatin1String("(0x") + QString::number(quintptr(qobject), 16);
+ QString objectName = qobject->objectName();
if (!objectName.isEmpty())
result += QLatin1String(", \"") + objectName + QLatin1Char('\"');
+ result += QLatin1Char(')');
+ return encode(result);
+ } else {
+ return encode(QString::fromUtf8(metaObject->className()) + QLatin1String("(0x0)"));
}
-
- result += QLatin1Char(')');
- } else {
- result = QLatin1String("null");
}
- return engine->newString(result)->asReturnedValue();
+ return encode(QLatin1String("null"));
}
ReturnedValue QObjectMethod::method_destroy(ExecutionEngine *engine, const Value *args, int argc) const