Discussion:
[Development] Override toString() method for c++ QObject types in QML
Luke Parry
2015-11-07 17:52:31 UTC
Permalink
Is it possible to override the toString method for native c++ QObject types
for use in QML scripts?

e.g. using

Q_INVOKABLE QString toString() const;

This doesn't appear to work because it is overridden by some default
implementation which returns a pointer value. Is this done intentionally?
Could a workaround be done through javascript prototype methods...

It would just improve the readability of debug messages and errors when
running scripts.

Huge Thanks,
Luke Parry
Konstantin Ritt
2015-11-07 22:45:59 UTC
Permalink
Maybe JSON.stringify(obj) is what you need?


Konstantin
Post by Luke Parry
Is it possible to override the toString method for native c++ QObject
types for use in QML scripts?
e.g. using
Q_INVOKABLE QString toString() const;
This doesn't appear to work because it is overridden by some default
implementation which returns a pointer value. Is this done intentionally?
Could a workaround be done through javascript prototype methods...
It would just improve the readability of debug messages and errors when
running scripts.
Huge Thanks,
Luke Parry
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Stephen Kelly
2015-11-08 21:57:31 UTC
Permalink
Post by Konstantin Ritt
Maybe JSON.stringify(obj) is what you need?
I think the intention is to override what that does using a specified
method.

Thanks,

Steve.
Knoll Lars
2015-11-09 09:10:56 UTC
Permalink
Post by Stephen Kelly
Post by Konstantin Ritt
Maybe JSON.stringify(obj) is what you need?
I think the intention is to override what that does using a specified
method.
JSON.stringify() does something slightly different than what you can do by reimplementing toString().

For toString(), the default implementation for the object prototype will currently get used. Implementing an invokable method on the wrapped QObject would be nice, but currently there is some logic in the engine missing that would allow that method to get picked up automatically.

Cheers,
Lars
Luke Parry
2015-11-10 11:12:08 UTC
Permalink
That's fine. I was curious if this was possible or not in the current state
of QML2.

I do not yet have critical need for it but it could also be useful for
overriding the valueOf prototype. I will just use another method to provide
debug information about a QObject type.

Thanks for the quick answer,
Luke
On 08/11/15 22:57, "Development on behalf of Stephen Kelly" <
Post by Stephen Kelly
Post by Konstantin Ritt
Maybe JSON.stringify(obj) is what you need?
I think the intention is to override what that does using a specified
method.
JSON.stringify() does something slightly different than what you can do by
reimplementing toString().
For toString(), the default implementation for the object prototype will
currently get used. Implementing an invokable method on the wrapped QObject
would be nice, but currently there is some logic in the engine missing that
would allow that method to get picked up automatically.
Cheers,
Lars
_______________________________________________
Development mailing list
http://lists.qt-project.org/mailman/listinfo/development
Loading...