Qt QML如何找到形状的宽度和高度?
我正在使用一个Qt QML如何找到形状的宽度和高度?,qt,qml,Qt,Qml,我正在使用一个形状,该形状是使用形状路径和路径SVG绘制的,如下所示: import QtQuick 2.12 import QtQuick.Controls 2.5 import QtQuick.Shapes 1.12 import QtQml 2.12 Item { property var m property color col: "white" Shape { id: ashape containsMode: Shap
形状
,该形状是使用形状路径
和路径SVG
绘制的,如下所示:
import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Shapes 1.12
import QtQml 2.12
Item
{
property var m
property color col: "white"
Shape
{
id: ashape
containsMode: Shape.FillContains
ShapePath
{
strokeWidth: 1
strokeColor: "transparent"
fillColor: col
PathSvg
{
path: m.path
}
}
}
Text
{
width: ashape.implicitWidth // these are zero
height: ashape.implicitHeight // so it doesnt work
color: "white"
text: "hello"
font.pointSize: 12
}
}
我想在形状上放置一些文本
,但无法获得文本放置的宽度和高度
有什么想法吗?
谢谢
更新
这是一个完整的例子。我希望文本显示在形状上
main.qml
import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Shapes 1.12
import QtQml 2.12
ApplicationWindow
{
visible: true
width: 640
height: 480
Shape
{
id: ashape
ShapePath
{
fillColor: "red"
PathSvg
{
path: "M100 100 l0 100 l100 0 Z"
}
}
Text
{
// want the text to appear over the shape
text: "hello"
font.pointSize: 12
}
}
}
main.cpp
#包括
#包括
#包括
int main(int argc,char*argv[])
{
QGUI应用程序应用程序(argc、argv);
qqmlaplicationengine;
engine.load(QUrl(QStringLiteral(“qrc:/main.qml”));
返回app.exec();
}
q、 专业的
更新2
上图
如我所见,
Shape
源自Item
,因此具有width()
和height()
等属性。但在这种情况下,我认为正确的声明方式是使用锚定,即必须将文本
放在形状
中,并设置锚定。centerIn:parent
。QtQuick将为您完成所有工作。@folibis很好的尝试,但没有成功<代码>形状没有大小,将锚定。fil:parent
放在形状中不起作用,因为项
没有大小<代码>项目没有大小,因为我不知道同样没有大小的形状路径
的大小。它画出了形状,但没有锚定到或其他大小。请您提供,以便我们可以运行和测试它?i、 e.m.path
等…@folibis谢谢。我增加了更多的例子。如果你把centerIn:parent
的想法放进去,很不幸,“hello”文本会从左上角移走。看起来你必须明确设置形状的大小,正如我看到的Shape
是从项衍生而来的,因此具有width()
和height()
等属性。但在这种情况下,我认为正确的声明方式是使用锚定,即必须将文本
放在形状
中,并设置锚定。centerIn:parent
。QtQuick将为您完成所有工作。@folibis很好的尝试,但没有成功<代码>形状
没有大小,将锚定。fil:parent
放在形状中不起作用,因为项
没有大小<代码>项目
没有大小,因为我不知道同样没有大小的形状路径
的大小。它画出了形状,但没有锚定到或其他大小。请您提供,以便我们可以运行和测试它?i、 e.m.path
等…@folibis谢谢。我增加了更多的例子。如果你把centerIn:parent
的想法放进去,很遗憾,“hello”文本会从左上角移走。看起来你必须明确设置形状的大小
TEMPLATE = app
QT += qml quick quickcontrols2
CONFIG += console
SOURCES += main.cpp
RESOURCES += qml.qrc