我正在使用Shape
和ShapePath
绘制的PathSvg
,如下所示:
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
}
}
我想在形状上放置一些Text
,但无法获得放置文本的宽度和高度。
有什么想法吗? 谢谢。
这是一个完整的示例。我希望文字显示在形状上;
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
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <qqmlcontext.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
q.pro
TEMPLATE = app
QT += qml quick quickcontrols2
CONFIG += console
SOURCES += main.cpp
RESOURCES += qml.qrc
上图;