簡體   English   中英

Qt Quick 2 Qml:將文本准確放置在圓心內

[英]Qt Quick 2 Qml: Place text exactly in center of circle

我正在嘗試將文本恰好放在Qml的圓圈中心:

Rectangle {
    id: rect
    width: 20
    height: 20
    radius: width/2
    color: "red"
    anchors.verticalCenter: parent.verticalCenter

    Label { // or Text
        anchors.centerIn: parent // or anchors.fill: parent
        text: "A"
        color: "white"
        verticalAlignment: Text.AlignVCenter
        horizontalAlignment: Text.AlignHCenter
    }

    //EDIT: For testing different locations:
    MouseArea {
        anchors.fill: parent        
        drag.target: rect
        drag.axis: Drag.XAxis
    }
}

問題在於文本略偏離中心(向左和向左移動)。 我測試了不同的大小和字符,但是都顯示了相同的問題。 有沒有更好的方法將文本准確地放置在中心?

[編輯] :字體偏移量似乎與屏幕上的位置有關(但我無法確定是什么原因導致的-它同時發生在奇數和偶數x坐標上)。 換句話說:當您在圓上拖動時,居中字符會稍微移動。

問候,

如果要放置一個項目,例如說項目A,位於另一個項目的確切中心,則應關閉A的anchors.alignWhenCentered (默認情況下為true,以便使項目清晰繪出)。

這是我的方法,雖然不是很優雅,但是可以用...

Text {
    y: -4
    text: "A"
    color: "white"
    font.pixelSize: 20
    anchors.horizontalCenter: rect.horizontalCenter
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM