簡體   English   中英

在沒有 Canvas 的 QML 中繪制虛線圓

[英]Draw a dashed circle in QML without Canvas

我想繪制一個虛線圓,其半徑將根據變量增加或縮小。 我只找到了一個基於Canvas的解決方案: Qt (QML) Dashed Circle

我有點驚訝沒有找到另一個解決方案。 恐怕Canvas解決方案會消耗太多資源。 有沒有其他實用的方法?

如果您不想使用 Canvas,另一種方法是使用QQuickPaintedItem並自己畫一個圓圈。 實現看起來像這樣:

dashcircle.h

#ifndef DASHCIRCLE_H
#define DASHCIRCLE_H

#include <QObject>
#include <QQuickPaintedItem>
#include <QPainter>

class DashCircle : public QQuickPaintedItem
{
    Q_OBJECT
public:
    explicit DashCircle(QQuickItem *parent = nullptr);

    virtual void paint(QPainter *painter);
};

#endif // DASHCIRCLE_H

dashcircle.cpp:

#include "dashcircle.h"

DashCircle::DashCircle(QQuickItem *parent) : QQuickPaintedItem(parent)
{

}

void DashCircle::paint(QPainter *painter)
{
    painter->setPen(QPen(Qt::DashLine));
    painter->drawEllipse(0, 0, width() - 1, height() - 1);
}

注冊類型:

qmlRegisterType<DashCircle>("Custom", 1, 0, "DashCircle");

在 qml 中創建:

DashCircle {
    x: 50
    y: 50
    width: 50
    height: 50
}

結果:

圓圈

您可以使用我在Qt (QML) Dashed Circle中提到的Qt Quick Shapes

暫無
暫無

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

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