Qt QML如何找到形状的宽度和高度?

时间:2019-03-17 20:05:53

标签: qt qml

我正在使用ShapeShapePath绘制的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

更新2

上图;

enter image description here

0 个答案:

没有答案