Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to declare metatypes in QML
Qt 6.11 is out! See what's new in the release blog

How to declare metatypes in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Pieter CardoenP Offline
    Pieter CardoenP Offline
    Pieter Cardoen
    wrote on last edited by Pieter Cardoen
    #1

    Dear

    I'm facing a problem with metatypes in QML. I have defined a custom metatype in c++:

    class Topic
    {
    Q_GADGET
    Q_PROPERTY(bool valid ...)
    }
    Q_DECLARE_METATYPE(Topic)
    

    In main, I have included this:

    qRegisterMetaType<Topic>("Topic");
    

    When propagating these elements from c++ to QML, everything is working. I can access the property.

    I however have a scenario where I want to declare such an object in qml and transmit it to the c++. For that, I want to declare a variable of type "Topic" and use it in a Q_INVOKABLE function. I however can't find a way to declare such a "Topic" object in qml.

    How do I do this?

    B 1 Reply Last reply
    1
    • Pieter CardoenP Pieter Cardoen

      Dear

      I'm facing a problem with metatypes in QML. I have defined a custom metatype in c++:

      class Topic
      {
      Q_GADGET
      Q_PROPERTY(bool valid ...)
      }
      Q_DECLARE_METATYPE(Topic)
      

      In main, I have included this:

      qRegisterMetaType<Topic>("Topic");
      

      When propagating these elements from c++ to QML, everything is working. I can access the property.

      I however have a scenario where I want to declare such an object in qml and transmit it to the c++. For that, I want to declare a variable of type "Topic" and use it in a Q_INVOKABLE function. I however can't find a way to declare such a "Topic" object in qml.

      How do I do this?

      B Offline
      B Offline
      Bob64
      wrote on last edited by
      #2

      @Pieter-Cardoen if I understand you correctly, I don't think it will be possible to do what you want to do. The C++ can only ever know about types declared in C++ at compile time.

      Pieter CardoenP 1 Reply Last reply
      0
      • B Bob64

        @Pieter-Cardoen if I understand you correctly, I don't think it will be possible to do what you want to do. The C++ can only ever know about types declared in C++ at compile time.

        Pieter CardoenP Offline
        Pieter CardoenP Offline
        Pieter Cardoen
        wrote on last edited by
        #3

        Dear @Bob64

        The topic goes about using the metatype in qml/javascript. I want to do this kind of things:

        MouseArea{
            onClicked: {
                Topic t // Declare variable -> this is what I want to do
                t.valid = false
                // use t in a Q_INVOKABLE function
            }
        }
        
        
        1 Reply Last reply
        1
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          Sadly this is not possible for the moment : https://bugreports.qt.io/browse/QTBUG-72223

          What you can do instead is have a c++ factory Q_INVOKABLE function returning a gadget:

          MouseArea{
              onClicked: {
                  let t = cppBackend.createTopic();
                  t.valid = false
                  // use t in a Q_INVOKABLE function
              }
          }
          
          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved