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. Recommended way to expose properties

Recommended way to expose properties

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 459 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.
  • G Offline
    G Offline
    Gustavo Serra
    wrote on last edited by
    #1

    Hello!

    What is the preferable way to expose a property of a reusable component?

    Suppose a message dialog in which the caller set the message.

    Caller code:

    Window {
        width: 640
        height: 480
        visible: true
    
        MyDialog {
            message: "My custom message"
        }
    }
    

    I could implement MyDialog as:

    Item {
        property alias message: inner.text
        Text { id: inner }
    }
    

    Or:

    Item {
        property string message
        Text {
            id: innerText
            text: message
        }
    }
    

    The way I see it, the second approach creates an extra binding but we preserve hierarchy (the parent doesn't depend on its children).

    johngodJ 1 Reply Last reply
    0
    • G Gustavo Serra

      @johngod thank you!

      L Offline
      L Offline
      lemons
      wrote on last edited by lemons
      #4

      @Gustavo-Serra
      I prefer the alias method, if I only need the property in a single location.

      As soon as I need to access the property in multiple locations within the component, I prefer option 2, as don't like binding nested objects to other objects if it isn't required.
      This way I can easily remove parts, without breaking the main component.

      E.g.

      Item {
          property color fontColor: "lime"
          Text { id: title; color: fontColor }
          Text { id: subtitle; color: fontColor }
      }
      
      // I don't like this approach, as I couldn't remove 
      // the title without effecting the subtitle
      Item {
          property alias fontColor: title.color
          Text { id: title; color: "lime" }
          Text { id: subtitle; color: title.color }
      }
      
      G 1 Reply Last reply
      1
      • G Gustavo Serra

        Hello!

        What is the preferable way to expose a property of a reusable component?

        Suppose a message dialog in which the caller set the message.

        Caller code:

        Window {
            width: 640
            height: 480
            visible: true
        
            MyDialog {
                message: "My custom message"
            }
        }
        

        I could implement MyDialog as:

        Item {
            property alias message: inner.text
            Text { id: inner }
        }
        

        Or:

        Item {
            property string message
            Text {
                id: innerText
                text: message
            }
        }
        

        The way I see it, the second approach creates an extra binding but we preserve hierarchy (the parent doesn't depend on its children).

        johngodJ Offline
        johngodJ Offline
        johngod
        wrote on last edited by
        #2

        @Gustavo-Serra
        I would use the first approach with the alias, I fail to see why not preservinh hierarchy would be a problem

        G 1 Reply Last reply
        0
        • johngodJ johngod

          @Gustavo-Serra
          I would use the first approach with the alias, I fail to see why not preservinh hierarchy would be a problem

          G Offline
          G Offline
          Gustavo Serra
          wrote on last edited by
          #3

          @johngod thank you!

          L 1 Reply Last reply
          0
          • G Gustavo Serra

            @johngod thank you!

            L Offline
            L Offline
            lemons
            wrote on last edited by lemons
            #4

            @Gustavo-Serra
            I prefer the alias method, if I only need the property in a single location.

            As soon as I need to access the property in multiple locations within the component, I prefer option 2, as don't like binding nested objects to other objects if it isn't required.
            This way I can easily remove parts, without breaking the main component.

            E.g.

            Item {
                property color fontColor: "lime"
                Text { id: title; color: fontColor }
                Text { id: subtitle; color: fontColor }
            }
            
            // I don't like this approach, as I couldn't remove 
            // the title without effecting the subtitle
            Item {
                property alias fontColor: title.color
                Text { id: title; color: "lime" }
                Text { id: subtitle; color: title.color }
            }
            
            G 1 Reply Last reply
            1
            • L lemons

              @Gustavo-Serra
              I prefer the alias method, if I only need the property in a single location.

              As soon as I need to access the property in multiple locations within the component, I prefer option 2, as don't like binding nested objects to other objects if it isn't required.
              This way I can easily remove parts, without breaking the main component.

              E.g.

              Item {
                  property color fontColor: "lime"
                  Text { id: title; color: fontColor }
                  Text { id: subtitle; color: fontColor }
              }
              
              // I don't like this approach, as I couldn't remove 
              // the title without effecting the subtitle
              Item {
                  property alias fontColor: title.color
                  Text { id: title; color: "lime" }
                  Text { id: subtitle; color: title.color }
              }
              
              G Offline
              G Offline
              Gustavo Serra
              wrote on last edited by
              #5

              @lemons
              It makes sense. I thought you should always do it in a certain way, but I can see now that it is a matter of preference or situation. Thank you.

              1 Reply Last reply
              0
              • G Gustavo Serra 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