[Qt-qml] Using C++ classes in signals on the QML side

Ademar de Souza Reis Jr. ademar.reis at openbossa.org
Mon Mar 29 15:33:34 CEST 2010


On Mon, Mar 29, 2010 at 02:11:02AM +0200, aaron.kennedy at nokia.com wrote:
> Hi,
> 
> On 27/03/10 7:48 AM, "ext Ademar de Souza Reis Jr."
> <ademar.reis at openbossa.org> wrote:
> 
> > Hi there.
> > 
> > Is there a way to use an exported C++ class as a parameter in a
> > signal sent to QML?
> > 
> > Following the code from examples/declarative/extending/signal/, this is what I
> > wan to do:
> > 
> > ["Boy" is a C++ class exported to QML]
> > 
> > birthdayparty.h:
> >  signals:
> >       void partyStarted(const QTime &time);
> > +    void newGuestString(const QString &str); // works
> > +    void newGuestBoy(const Boy &guest); // doesn't
> > 
> > example.qml:
> >  BirthdayParty {
> >      onPartyStarted: console.log("This party started rockin' at " + time);
> > +    onNewGuestBoy: console.log("There's a new guest (boy): " + guest.name);
> > // doesn't work
> > +    onNewGuestString: console.log("There's a new guest (string): " + str); //
> > works
> > 
> > (simplified diff, you get the idea)
> > 
> > This is the output when the three signals are emitted from the C++ code:
> > 
> > """
> > This party started rockin' at 18:16:16
> > QMetaProperty::read: Unable to handle unregistered datatype 'Person' for
> > property 'QDeclarativeBoundSignalParameters::guest'
> > file::example.qml:7: TypeError: Result of expression 'guest' [undefined] is
> > not an object.
> > There's a new guest (string): foobar
> > """
> > 
> > So as you can see using QTime and QString works, but using Boy doesn't. Any
> > ideas?
> 
> Assuming Boy is a C++ class exposed to QML as you say, emitting it as a
> pointer parameter should work.  For example
> 
> signals:
>     void newGuestBoy(const Boy *guest)
> 

It doesn't work either:

"""
QMetaProperty::read: Unable to handle unregistered datatype 'const Boy*' for property
'QDeclarativeBoundSignalParameters::guest'
"""

I'm using the signal example from Qt sources
(declarative/extending/signal). The code that exports Boy
is part of the example (I can access and write to Boy
objects in example.qml, so I assume it's working).

Attached is the diff.

I guess I should file a bug?

Thanks,
  - Ademar

-- 
Ademar de Souza Reis Jr. <ademar.reis at openbossa.org>
OpenBossa Labs - Instituto Nokia de Tecnologia (INdT)
http://www.openbossa.org/
http://www.indt.org.br/

^[:wq!
-------------- next part --------------
diff --git a/examples/declarative/extending/signal/birthdayparty.cpp b/examples/declarative/extending/signal/birthdayparty.cpp
index 65ff530..6d052f5 100644
--- a/examples/declarative/extending/signal/birthdayparty.cpp
+++ b/examples/declarative/extending/signal/birthdayparty.cpp
@@ -90,6 +90,14 @@ void BirthdayParty::startParty()
 {
     QTime time = QTime::currentTime();
     emit partyStarted(time);
+
+    // XXX
+    Boy *foo = new Boy();
+    foo->setName("foobar");
+
+    emit newGuestBoyPointer(foo);
+    emit newGuestBoyRef(*foo);
+    emit newGuestString("bla");
 }
 
 BirthdayPartyAttached *BirthdayParty::qmlAttachedProperties(QObject *object)
diff --git a/examples/declarative/extending/signal/birthdayparty.h b/examples/declarative/extending/signal/birthdayparty.h
index bcdc513..c4b857c 100644
--- a/examples/declarative/extending/signal/birthdayparty.h
+++ b/examples/declarative/extending/signal/birthdayparty.h
@@ -83,6 +83,9 @@ public:
 // ![0]
 signals:
     void partyStarted(const QTime &time);
+    void newGuestString(const QString &str); // works
+    void newGuestBoyRef(const Boy &guest); // doesn't
+    void newGuestBoyPointer(const Boy *guest); // doesn't
 // ![0]
 
 private:
diff --git a/examples/declarative/extending/signal/example.qml b/examples/declarative/extending/signal/example.qml
index c7d4792..1d5d583 100644
--- a/examples/declarative/extending/signal/example.qml
+++ b/examples/declarative/extending/signal/example.qml
@@ -3,6 +3,9 @@ import People 1.0
 // ![0]
 BirthdayParty {
     onPartyStarted: console.log("This party started rockin' at " + time);
+    onNewGuestString: console.log("There's a new guest: " + str); // works
+    onNewGuestBoyPointer: console.log("There's a new guest: " + guest.name); // doesn't work
+    onNewGuestBoyRef: console.log("There's a new guest: " + guest.name); // doesn't work
 // ![0]
 
     celebrant: Boy {


More information about the Qt-qml mailing list