Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-90714

Exposing C++ object to QML - the right way

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • P2: Important
    • None
    • 6.0.0
    • None
    • Qt Creator 4.14.0,
      Ubuntu 20.04,
      Qt 6.0.0.
    • All

    Description

      I have a trouble to expose my C++ class to QML. So far I didn't find some description in the Qt documentation  how to do that right, especially the type of the exposed object. The common C++ way to pass a const reference but that doesn't work for me.

      Talking in detail, I have an object (Provider) that I exposed to QML using context property. That works fine. The object has a signal that passes another object (Message) that I've registered using `qmlRegisterUncreatableType`. But what should be a signature of the signal? So far I've tried:

      void messageReceived(const Message &message);

      The application crashes inside QMetaTypeInterface::DtorFn getDtor()

       

      void messageReceived(Message &message);

      The same as above.

       

      void messageReceived(const Message *message);

      I get en error: TypeError: Property 'getText' of object QVariant(Message, ) is not a function

       

      void messageReceived(Message *message);

      This is the only option that works. Why? Is that bug? a feature? Actually I prefer not to pass non-const object if I don't want to change it inside. As I said the common C++ way is to pass const reference. Why doesn't that work?

       

      P.S. After changing the Message to be Q_GADGET all that stops to work at all. The message I get in console:

      qt.qml.typeregistration: Invalid QML element name "Message"; value type names should begin with a lowercase letter
      qrc:/main.qml:15: TypeError: Property 'getText' of object QVariant(Message*, 0x7fffffffb677) is not a function

      What? Why that works fine with Q_OBJECT and doesn't work with Q_GADGET? Ok, Ok, changing `Message` to `message` doesn't solve the issue. The error message still:

      TypeError: Property 'getText' of object QVariant(message*, 0x7fffffffb677) is not a function

      I really don't understand what I do wrong. I guess that works fine in Qt < 6.0 but can't check it now.

       

       

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              qt.team.quick.subscriptions Qt Quick and Widgets Team
              folibis ruslan
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes