Important: Please read the Qt Code of Conduct -

anchoring qml text item using baseline

  • hello ,

    I am not able to understand how to anchor qml text item using anchors.baseline property.
    Lets say i have rectangular box 100x200 size and i want to place text item on baseline (y= 40). how can i achieve that?
    currently i am assigning anchors.baseline to (y = 40) which is a line parallel to x axis at a height of 40 on which i want my text item to sit.
    and what is the use of anchors.baselineOffset ?? i am confused !!

    id: name
    x: 316
    anchors.baseline: (y = 40)
    text = "Hello!"
    font.pixelSize: 16

  • This should help understanding the various anchors.

    Not sure about the baseline thing your trying. But docu verbatim; says:

    The baseline (not pictured above) corresponds to the imaginary line on which text would sit. For items with no text it is the same as top.

    What about in your text item:

    anchors { bottom: parent.bottom; bottomMargin : 40; }

    You are:

    • setting x to out of range for your example of 100*200
    • manually setting position with x and anchors is probably going to end up badly.

    If you want precise control you can use x sure.
    You can bind x (to an expression, another object, many things)

    I'd hazard a guess you have a little bit of reading for the basics - there's a QML book @ that helped when I was starting and sometimes look at.

  • hi 6thc thanks for your answer ,i have a very basic question here..
    i just want to know how to assign the "anchors.baseline" and "anchors.baselineOffse" properties in text without actually using some other item's anchors.

    look at example i provided below. now i am not using x, y positions ,as you can see only anchors are used.

    option1: this works fine to keep text1 basline at y=40 relative to parentRect.

    id: parentRect

    id: rect1

    x: 0
    y: 40
    height: 1
    width = 200
    color = "white"

    id: text1
    anchors.left : parent.left
    anchors.baseline :

    option 2: what if there is no other sibling rectangle, what should be the value to pass to anchors.baseline property of text1 tokeep text1 basline at y=40 relative to parentRect ? and again dont use or bottom and give baslineOffset that works too. i just wanted to learn about QQuickAnchorLine class objects
    id: parentRect
    id: text1
    anhcors.left: parent.left
    anchors.baseline = ?

  • i guess i was not clear about my question , currently works well using baseline and baslineoffset .
    how can i move this to solved?

Log in to reply