Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. What is Q_PROPERTY's equivalent to QML default properties?
Forum Updated to NodeBB v4.3 + New Features

What is Q_PROPERTY's equivalent to QML default properties?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 2 Posters 718 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    I have found the "default" attribute for properties defined in QML.

    I would like to make a property of a C++ Q_GADGET a default property, but I can't seem to find the right keyword in Q_PROPERTY.

    kkoehneK 1 Reply Last reply
    1
    • A Asperamanca

      I have found the "default" attribute for properties defined in QML.

      I would like to make a property of a C++ Q_GADGET a default property, but I can't seem to find the right keyword in Q_PROPERTY.

      kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      @Asperamanca

      It's

      Q_CLASSINFO("DefaultProperty", "propertyname")
      

      See e.g. https://doc.qt.io/qt-6.2/qtqml-referenceexamples-default-example.html

      Director R&D, The Qt Company

      A 1 Reply Last reply
      3
      • kkoehneK kkoehne

        @Asperamanca

        It's

        Q_CLASSINFO("DefaultProperty", "propertyname")
        

        See e.g. https://doc.qt.io/qt-6.2/qtqml-referenceexamples-default-example.html

        A Offline
        A Offline
        Asperamanca
        wrote on last edited by Asperamanca
        #3

        @kkoehne Wow, that's really hidden in an obscure place in the docs...
        Thanks a lot!

        kkoehneK 1 Reply Last reply
        0
        • A Asperamanca has marked this topic as solved on
        • A Asperamanca

          @kkoehne Wow, that's really hidden in an obscure place in the docs...
          Thanks a lot!

          kkoehneK Offline
          kkoehneK Offline
          kkoehne
          Moderators
          wrote on last edited by
          #4

          @Asperamanca To be fair, it's also explained in

          https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html

          Where did you look for this information? Just so that we can add some cross-references ...

          Director R&D, The Qt Company

          A 1 Reply Last reply
          0
          • kkoehneK kkoehne

            @Asperamanca To be fair, it's also explained in

            https://doc.qt.io/qt-6/qtqml-cppintegration-definetypes.html

            Where did you look for this information? Just so that we can add some cross-references ...

            A Offline
            A Offline
            Asperamanca
            wrote on last edited by Asperamanca
            #5

            @kkoehne
            I looked in the explanation of the Qt Property system, and searched the whole page for "default". I also looked for links to further information especially around the section describing Q_PROPERTY

            A 1 Reply Last reply
            0
            • A Asperamanca

              @kkoehne
              I looked in the explanation of the Qt Property system, and searched the whole page for "default". I also looked for links to further information especially around the section describing Q_PROPERTY

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              @Asperamanca But I see, this default property is a QML-specific thing, so it doesn't belong into the general explanation of the Qt Property System. Maybe a link to the QML-specific parts would help.

              kkoehneK 1 Reply Last reply
              0
              • A Asperamanca

                @Asperamanca But I see, this default property is a QML-specific thing, so it doesn't belong into the general explanation of the Qt Property System. Maybe a link to the QML-specific parts would help.

                kkoehneK Offline
                kkoehneK Offline
                kkoehne
                Moderators
                wrote on last edited by
                #7

                @Asperamanca Created https://codereview.qt-project.org/c/qt/qtbase/+/526494 for now. let's see.

                Director R&D, The Qt Company

                A 1 Reply Last reply
                0
                • kkoehneK kkoehne

                  @Asperamanca Created https://codereview.qt-project.org/c/qt/qtbase/+/526494 for now. let's see.

                  A Offline
                  A Offline
                  Asperamanca
                  wrote on last edited by Asperamanca
                  #8

                  @kkoehne However, does this even work for properties of value types?

                  I have something like

                  namespace MyModule
                  {
                  
                  class IntWrapper
                  {
                      Q_GADGET
                      QML_VALUE_TYPE(myModule_IntWrapper)
                      Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue)
                      Q_CLASSINFO("DefaultProperty", "intValue")
                  public:
                    int m_Value = {};
                    int getValue() const;
                    void setValue(const int& arg);
                  };
                  } // namespace MyModule
                  

                  And in QML:

                  import MyModule;
                  
                  ComboBox
                  {
                    readonly property myModule_IntWrapper testInt: highlightedIndex
                  }
                  

                  I would expect testInt to be initialized with the highlightedIndex, but instead I get
                  Unable to assign int to MyModule::IntWrapper.

                  I can do this:

                  ComboBox
                  {
                    property myModule_IntWrapper testInt: highlightedIndex
                    onHighlightedIndexChanged: testInt.intValue  = highlightedIndex
                  }
                  

                  ...but that's not very elegant.

                  A 1 Reply Last reply
                  0
                  • A Asperamanca

                    @kkoehne However, does this even work for properties of value types?

                    I have something like

                    namespace MyModule
                    {
                    
                    class IntWrapper
                    {
                        Q_GADGET
                        QML_VALUE_TYPE(myModule_IntWrapper)
                        Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue)
                        Q_CLASSINFO("DefaultProperty", "intValue")
                    public:
                      int m_Value = {};
                      int getValue() const;
                      void setValue(const int& arg);
                    };
                    } // namespace MyModule
                    

                    And in QML:

                    import MyModule;
                    
                    ComboBox
                    {
                      readonly property myModule_IntWrapper testInt: highlightedIndex
                    }
                    

                    I would expect testInt to be initialized with the highlightedIndex, but instead I get
                    Unable to assign int to MyModule::IntWrapper.

                    I can do this:

                    ComboBox
                    {
                      property myModule_IntWrapper testInt: highlightedIndex
                      onHighlightedIndexChanged: testInt.intValue  = highlightedIndex
                    }
                    

                    ...but that's not very elegant.

                    A Offline
                    A Offline
                    Asperamanca
                    wrote on last edited by Asperamanca
                    #9

                    @Asperamanca
                    So I solved this using an undocumented feature:

                    namespace MyModule
                    {
                    
                    class IntWrapper
                    {
                        Q_GADGET
                        QML_VALUE_TYPE(myModule_IntWrapper)
                        Q_PROPERTY(int intValue READ getIntValue WRITE setIntValue)
                        QML_STRUCTURED_VALUE
                    public:
                      Q_INVOKABLE IntWrapper(const int& value);
                      int m_Value = {};
                      int getValue() const;
                      void setValue(const int& arg);
                    };
                    } // namespace MyModule
                    

                    EDIT: This is going to be documented in Qt 6.7, see https://bugreports.qt.io/browse/QTBUG-113490?jql=text ~ "QML_STRUCTURED_VALUE"

                    1 Reply Last reply
                    0

                    • Login

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