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. Subproperty not accessible
Qt 6.11 is out! See what's new in the release blog

Subproperty not accessible

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.6k 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.
  • kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #1

    Hello!
    Being green as grass, I am experimenting with QML and QuickControls 2 ... so I decided to "customize" the available tool button to display an image and text under it. So far so good. I was able to do what I want, but extending my component I stumbled on some very strange behavior, I can't access properties of a QtObject I use as property I've declared in my component. Here's the whole code:

    import QtQuick 2.7
    import QtQuick.Controls 2.0
    
    Item {
        id: root
        width: button.width + padding.left + padding.right
        height: button.height + padding.top + padding.bottom
    
        signal clicked()
    
        property alias text: buttonText.text
        property alias image: buttonImage
    
        property QtObject padding: QtObject {
            property int left: 0
            property int right: 0
            property int top: 0
            property int bottom: 0
        }
    
        ToolButton {
            id: button
    
            anchors.fill: parent
            anchors.leftMargin: padding.left
            anchors.topMargin: padding.top
            anchors.rightMargin: padding.right
            anchors.bottomMargin: padding.bottom
    
            width: indicator.width
            height: indicator.height
    
            indicator: Item {
                width: buttonImage.width > buttonText.contentWidth ? buttonImage.width : buttonText.contentWidth
                height: buttonImage.height + buttonText.contentHeight
    
                Image {
                    id: buttonImage
                    anchors.top: parent.top
                    anchors.left: parent.left
                    anchors.leftMargin: buttonText.contentWidth > width ? (buttonText.contentWidth - width) / 2 : 0
                    anchors.right: parent.right
                    anchors.rightMargin: anchors.leftMargin
                }
    
                Text {
                    id: buttonText
                    anchors.top: buttonImage.bottom
                    anchors.left: parent.left
                    anchors.leftMargin: buttonImage.width > contentWidth ? (buttonImage.width - contentWidth) / 2 : 0
                    anchors.right: parent.right
                    anchors.rightMargin: anchors.leftMargin
                    anchors.bottom: parent.bottom
                }
            }
    
            onClicked: parent.clicked()
        }
    }
    

    I can then create my tool buttons, but I get a "Cannot assign to non-existent property ..." error when I try to touch the padding property's properties, like this:

    MyToolButton {
        text: qsTr("Button text")
        image.source: "qrc:/my/image/path"
        image.height: 32
        image.width: 32
        // Problematic part:
        padding.left: 5
        padding.right: 5
        padding.top: 5
        padding.bottom: 10
    }
    

    What am I missing here? It's strange as the image property works without issue whatsoever ...
    Thanks in advance.

    Kind regards.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kofr
      wrote on last edited by Kofr
      #2

      you have not declared padding

      try smth like

      property var padding : {left:0, right:0, top:0, bottom:0} 
      

      however it is strange behavior with QtObject

      kshegunovK 1 Reply Last reply
      0
      • K Kofr

        you have not declared padding

        try smth like

        property var padding : {left:0, right:0, top:0, bottom:0} 
        

        however it is strange behavior with QtObject

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #3

        @Kofr
        Maybe you're right, but I blame my incompetence for the problem. Finally, after reading a bit in the documentation, I just reworked the code and I don't have the problem anymore.

        Here's the much better working (and much shorter) tool button:

        import QtQuick 2.7
        import QtQuick.Controls 2.0
        
        ToolButton {
            id: root
        
            property alias image: buttonImage
        
            contentItem: Item {
                Image {
                    id: buttonImage
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.top: parent.top
                    opacity: root.enabled ? 1.0 : 0.3
                }
        
                Text {
                    id: buttonText
        
                    text: root.text
                    font: root.font
        
                    anchors.horizontalCenter: buttonImage.horizontalCenter
                    anchors.top: buttonImage.bottom
                    anchors.topMargin: root.spacing
        
                    opacity: root.enabled ? 1.0 : 0.3
                    horizontalAlignment: Text.AlignHCenter
                    elide: Text.ElideRight
                }
            }
        
            background.implicitWidth: Math.max(buttonImage.width, buttonText.contentWidth) + leftPadding + rightPadding + buttonText.anchors.topMargin
            background.implicitHeight: buttonImage.height + buttonText.contentHeight + topPadding + bottomPadding
        }
        

        Read and abide by the Qt Code of Conduct

        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