Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt Quick 2 Qml:将文本精确放置在圆的中心_C++_Qt_Qml_Qtquick2 - Fatal编程技术网

C++ Qt Quick 2 Qml:将文本精确放置在圆的中心

C++ Qt Quick 2 Qml:将文本精确放置在圆的中心,c++,qt,qml,qtquick2,C++,Qt,Qml,Qtquick2,我正在尝试将文本精确放置在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

我正在尝试将文本精确放置在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的
锚。居中对齐(默认情况下这是正确的,以便使项目画得清晰)。

这是我的方式,不是很优雅,但它可以工作

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

我已经尝试了你的代码,但我没有观察到问题,你可以将一个图像放置在可以观察到问题的位置。尝试了你的代码。只有当圆非常小时,偏移才会明显。我相信您看到的偏移量是由于您使用的字体的自然字符间距造成的。你可以试着找一种更对称的字体。请参阅:您可以尝试使用
锚定。margin
来更正偏移量,但在这种情况下,您应该明确指定您的字体系列。谢谢您的回答。我编辑了这个问题,因为问题似乎与屏幕上的位置有关(看起来像是舍入或浮点/int错误)。这似乎确实解决了问题。