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. Why can't I define properties called "top", "bottom", "left", and "right"?
Forum Update on Monday, May 27th 2025

Why can't I define properties called "top", "bottom", "left", and "right"?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 3 Posters 619 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.
  • P Offline
    P Offline
    pderocco
    wrote on last edited by
    #1

    I'm trying to create a variant on a Rectangle that is positioned by setting its left, right, top, and bottom edge coordinates, rather than its x, width, y, and height. I'm using int properties so that they'll be rounded to integers. So I did this, in a file called RectInt.qml:

    import QtQuick 2.11
    
    Rectangle {
    
        property int    left
        property int    right
        property int    top
        property int    bottom
    
        x:      left
        y:      top
        width:  right - left
        height: bottom - top
    }
    

    I get the following error:
    qrc:/RectInt.qml:5 Cannot override FINAL property

    If I simply try setting properties of these names on a regular Rectangle, it complains that I'm trying to set read-only properties.

    If I change my definition to:

    import QtQuick 2.11
    
    Rectangle {
    
        property int    l
        property int    r
        property int    t
        property int    b
    
        x:      l
        y:      t
        width:  r - l
        height: b - t
    }
    

    then it works.

    This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?

    J.HilkJ 1 Reply Last reply
    0
    • P pderocco

      I'm trying to create a variant on a Rectangle that is positioned by setting its left, right, top, and bottom edge coordinates, rather than its x, width, y, and height. I'm using int properties so that they'll be rounded to integers. So I did this, in a file called RectInt.qml:

      import QtQuick 2.11
      
      Rectangle {
      
          property int    left
          property int    right
          property int    top
          property int    bottom
      
          x:      left
          y:      top
          width:  right - left
          height: bottom - top
      }
      

      I get the following error:
      qrc:/RectInt.qml:5 Cannot override FINAL property

      If I simply try setting properties of these names on a regular Rectangle, it complains that I'm trying to set read-only properties.

      If I change my definition to:

      import QtQuick 2.11
      
      Rectangle {
      
          property int    l
          property int    r
          property int    t
          property int    b
      
          x:      l
          y:      t
          width:  r - l
          height: b - t
      }
      

      then it works.

      This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @pderocco said in Why can't I define properties called "top", "bottom", "left", and "right"?:

      This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?

      it's not undocumented:

      http://doc.qt.io/qt-5/qtquick-positioning-anchors.html


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      JKSHJ P 2 Replies Last reply
      4
      • J.HilkJ J.Hilk

        @pderocco said in Why can't I define properties called "top", "bottom", "left", and "right"?:

        This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?

        it's not undocumented:

        http://doc.qt.io/qt-5/qtquick-positioning-anchors.html

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @J.Hilk said in Why can't I define properties called "top", "bottom", "left", and "right"?:

        @pderocco said in Why can't I define properties called "top", "bottom", "left", and "right"?:

        This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?

        it's not undocumented:

        http://doc.qt.io/qt-5/qtquick-positioning-anchors.html

        To clarify @J-Hilk's comment: It is not obvious from the documentation or the QML code, but left is linked to anchors.left.

        left, right, etc. are implemented as "private", read-only properties of QQuickItem: https://code.woboq.org/qt5/qtdeclarative/src/quick/items/qquickitem.h.html#122 (Rectangle inherits Item which instantiates QQuickItem)

        This looks like an unfortunate case of leaky implementation details.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @pderocco said in Why can't I define properties called "top", "bottom", "left", and "right"?:

          This suggests that QML defines undocumented read-only properties of those names, that probably do what one would expect (without coercing to ints, as I want). But the online 2.11 docs don't mention these names anywhere, and Google doesn't turn up any mention of them here, or on StackOverflow, either. What's going on?

          it's not undocumented:

          http://doc.qt.io/qt-5/qtquick-positioning-anchors.html

          P Offline
          P Offline
          pderocco
          wrote on last edited by
          #4

          @J.Hilk
          I see now that the anchors documentation mentions those names as properties of "parent", but I wouldn't call that "documentation" of those properties. That's an easy-to-miss reference to undocumented properties.

          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