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. What is the purpose of attached properties in QML?

What is the purpose of attached properties in QML?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 1.4k Views 2 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.
  • R Offline
    R Offline
    Richard Lee
    wrote on last edited by
    #1

    In the QT help document, it is said that the attached properties allows users to extend existing objects with additional attributes. However, if we want to do so, why not we just derive a C++ class, add new Q_PROPERTY properties and then register it as a new QML type?

    For instance, in the BirthdayParty example, attached property "QDate rsvp" in the BirthdayPartyAttached class can actually be added directly in the BirthdayParty class. I did not see any sense of declare an additional class to extend the BirthdayParty QML type.

    Maybe I am wrong, can anyone give an in-depth explanation?

    1 Reply Last reply
    0
    • jpnurmiJ Offline
      jpnurmiJ Offline
      jpnurmi
      wrote on last edited by
      #2

      ListView is a good example. It allows any Item type as a delegate. You can use Rectangles, Images, Rows, MouseAreas, Buttons, anything you wish. By using the attached property mechanism, ListView is able to provide ListView and delegate instance specific properties for the delegates, regardless of their exact type.

      If subclassing had been used to provide this information, you would be only able to use a dedicated ListViewItem type and you would always have to add your content as children of the extra wrapper items. It would be an annoying limitation API-wise, and it would cause unnecessary overhead by creating deeper item-hierarchy.

      1 Reply Last reply
      3

      • Login

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