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. Attached property VS Q_EXTENDED: add custom properties to a general QML Control Item
QtWS25 Last Chance

Attached property VS Q_EXTENDED: add custom properties to a general QML Control Item

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 733 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.
  • N Offline
    N Offline
    nico88desmo
    wrote on last edited by
    #1

    Dear all,

    I'm looking for add properties to a generic QML Control Item

    Supposing I have a class similar to this one:

    class ExtraProperties : public QObject {
        Q_OBJECT
    
    public:
        Q_PROPERTY(QString placeholderText READ getPlaceholderText WRITE setPlaceholderText NOTIFY placeholderTextChanged)
        Q_PROPERTY(QString helperText READ getHelperText WRITE setHelperText NOTIFY helperTextChanged)
    
    private:
        QString placeholderText;
        QString helperText;
    
    public:
        explicit ExtraProperties(QObject* parent = nullptr);
    
        QString getPlaceholderText() const;
        void setPlaceholderText(const QString& newPlaceholderText);
    
        QString getHelperText() const;
        void setHelperText(const QString& newHelperText);
    
    signals:
        void placeholderTextChanged();
        void helperTextChanged();
    };
    

    I'd like to adding these two properties (placeholderText and helperText) to some QML components such as:

    • TextEdit
    • SpinBox
    • ecc..

    In Qt docs, I see there are mainly two possibilities to reach this target:

    1. using Q_EXTEND
      This is the solution I'd prefer, however this isn't feasible because I haven't access to QQuickTextEdit headers, because they are private; so this code couldn't work (following this example)
       struct QLineEditForeign {
          Q_GADGET
          QML_FOREIGN(QQuickTextEdit) <--- KO, no headers to QuickTextEdit
          QML_NAMED_ELEMENT(TextEdit)
          QML_EXTENDED(ExtraProperties)
       };
    
    1. using attached-properties
      The problem of attaching properties is that I'm not able to use them outside of item, for example:
       class ExtraPropertiesAtt : public QObject {
          Q_OBJECT
          QML_ATTACHED(ExtraProperties)
          QML_ELEMENT
       public:
          static ExtraProperties* qmlAttachedProperties(QObject *object) {
             return new ExtraProperties(object);
          }
       };
    

    From QML then

    • CustomItem.qml
    Item {
       ...
       ExtraPropertiesAtt.placeholderText: "Sample"
       ...
    }
    
    • Main.qml
    import CustomItem
    
    Window {
       CustomItem {
          ExtraPropertiesAtt.placeholderText: "Test" <--- KO
       }
    }
    

    So, are there any other ways to achieve this goal?

    Thanks,
    Nicola

    GrecKoG 1 Reply Last reply
    0
    • N nico88desmo

      Dear all,

      I'm looking for add properties to a generic QML Control Item

      Supposing I have a class similar to this one:

      class ExtraProperties : public QObject {
          Q_OBJECT
      
      public:
          Q_PROPERTY(QString placeholderText READ getPlaceholderText WRITE setPlaceholderText NOTIFY placeholderTextChanged)
          Q_PROPERTY(QString helperText READ getHelperText WRITE setHelperText NOTIFY helperTextChanged)
      
      private:
          QString placeholderText;
          QString helperText;
      
      public:
          explicit ExtraProperties(QObject* parent = nullptr);
      
          QString getPlaceholderText() const;
          void setPlaceholderText(const QString& newPlaceholderText);
      
          QString getHelperText() const;
          void setHelperText(const QString& newHelperText);
      
      signals:
          void placeholderTextChanged();
          void helperTextChanged();
      };
      

      I'd like to adding these two properties (placeholderText and helperText) to some QML components such as:

      • TextEdit
      • SpinBox
      • ecc..

      In Qt docs, I see there are mainly two possibilities to reach this target:

      1. using Q_EXTEND
        This is the solution I'd prefer, however this isn't feasible because I haven't access to QQuickTextEdit headers, because they are private; so this code couldn't work (following this example)
         struct QLineEditForeign {
            Q_GADGET
            QML_FOREIGN(QQuickTextEdit) <--- KO, no headers to QuickTextEdit
            QML_NAMED_ELEMENT(TextEdit)
            QML_EXTENDED(ExtraProperties)
         };
      
      1. using attached-properties
        The problem of attaching properties is that I'm not able to use them outside of item, for example:
         class ExtraPropertiesAtt : public QObject {
            Q_OBJECT
            QML_ATTACHED(ExtraProperties)
            QML_ELEMENT
         public:
            static ExtraProperties* qmlAttachedProperties(QObject *object) {
               return new ExtraProperties(object);
            }
         };
      

      From QML then

      • CustomItem.qml
      Item {
         ...
         ExtraPropertiesAtt.placeholderText: "Sample"
         ...
      }
      
      • Main.qml
      import CustomItem
      
      Window {
         CustomItem {
            ExtraPropertiesAtt.placeholderText: "Test" <--- KO
         }
      }
      

      So, are there any other ways to achieve this goal?

      Thanks,
      Nicola

      GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote on last edited by
      #2

      @nico88desmo How does the "KO" occur in your last snippet?

      N 1 Reply Last reply
      0
      • GrecKoG GrecKo

        @nico88desmo How does the "KO" occur in your last snippet?

        N Offline
        N Offline
        nico88desmo
        wrote on last edited by
        #3

        @GrecKo from my first test code, it seems that CustomItem's placeholderText value is set equals to Item's placeholderText, ignoring the assignment in Main.qml.

        That's why I wrote KO; it's quite strange... unless I'm doing some other errors in other code parts.

        N 1 Reply Last reply
        0
        • N nico88desmo

          @GrecKo from my first test code, it seems that CustomItem's placeholderText value is set equals to Item's placeholderText, ignoring the assignment in Main.qml.

          That's why I wrote KO; it's quite strange... unless I'm doing some other errors in other code parts.

          N Offline
          N Offline
          nico88desmo
          wrote on last edited by
          #4

          @nico88desmo said in Attached property VS Q_EXTENDED: add custom properties to a general QML Control Item:

          @GrecKo from my first test code, it seems that CustomItem's placeholderText value is set equals to Item's placeholderText, ignoring the assignment in Main.qml.

          That's why I wrote KO; it's quite strange... unless I'm doing some other errors in other code parts.

          @GrecKo I've rechecked the code, and effectively the problem was that in my real code (I've posted just a snippet in order to no put boilerplate code inside forum page), Item is inside a plugin and Main.qml is in the main application.

          The problem was quite stupid: plugin wasn't recompiled so Main.qml uses an old Item's version.

          Now attached property works as expected

          1 Reply Last reply
          0
          • N nico88desmo has marked this topic as solved on

          • Login

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