qml绘图的三种方式

网友投稿 1332 2022-10-18

qml绘图的三种方式

qml绘图的三种方式

qml绘图的三种方式

1,继承QQuickPaintedItem重写void paint(QPainter *painter);

#include "PaintedItem.h"#include #include #include #include #include PaintedItem::PaintedItem(QQuickItem *parent) : QQuickPaintedItem(parent) , m_element(0) , m_bEnabled(true) , m_bPressed(false) , m_bMoved(false) , m_pen(Qt::black){ setAcceptedMouseButtons(Qt::LeftButton);}PaintedItem::~PaintedItem(){ purgePaintElements();}void PaintedItem::clear(){ purgePaintElements(); update();}void PaintedItem::undo(){ if(m_elements.size()) { delete m_elements.takeLast(); update(); }}void PaintedItem::paint(QPainter *painter){ painter->setRenderHint(QPainter::Antialiasing); int size = m_elements.size(); ElementGroup *element; for(int i = 0; i < size; ++i) { element = m_elements.at(i); painter->setPen(element->m_pen); painter->drawLines(element->m_lines); }}void PaintedItem::mousePressEvent(QMouseEvent *event){ m_bMoved = false; if(!m_bEnabled || !(event->button() & acceptedMouseButtons())) { QQuickPaintedItem::mousePressEvent(event); } else { m_bPressed = true; m_element = new ElementGroup(m_pen); m_elements.append(m_element); m_lastPoint = event->localPos(); event->setAccepted(true); }}void PaintedItem::mouseMoveEvent(QMouseEvent *event){ if(!m_bEnabled || !m_bPressed || !m_element) { QQuickPaintedItem::mousePressEvent(event); } else { //qDebug() << "mouse move"; m_element->m_lines.append(QLineF(m_lastPoint, event->localPos())); m_lastPoint = event->localPos(); update(); }}void PaintedItem::mouseReleaseEvent(QMouseEvent *event){ if(!m_element || !m_bEnabled || !(event->button() & acceptedMouseButtons())) { QQuickPaintedItem::mousePressEvent(event); } else { //qDebug() << "mouse released"; m_bPressed = false; m_bMoved = false; m_element->m_lines.append(QLineF(m_lastPoint, event->localPos())); update(); }}void PaintedItem::purgePaintElements(){ int size = m_elements.size(); if(size > 0) { for(int i = 0; i < size; ++i) { delete m_elements.at(i); } m_elements.clear(); } m_element = 0;}

效果如下:

2,继承QQuickItem重写QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);配合QSGNode实现:

#include "ordinateitem.h"#include #include #include namespace UtAbCapb {OrdinateItem::OrdinateItem(){ setFlag(ItemHasContents, true); this->init();}void OrdinateItem::init(){ m_scale_vp = LeftVP; m_line_width = 1; m_line_color = "#C5AD63"; m_ruler_count = 35;}int OrdinateItem::scaleViewPostion(){ return m_scale_vp;}void OrdinateItem::setScaleViewPostion(int vp){ m_scale_vp = vp;}int OrdinateItem::lineWidth(){ return m_line_width;}void OrdinateItem::setLineWidth(int width){ m_line_width = width;}QString OrdinateItem::lineColor(){ return m_line_color;}void OrdinateItem::setLineColor(QString color){ m_line_color = color;}int OrdinateItem::rulerTotal(){ return m_ruler_count;}void OrdinateItem::setRulerTotal(int count){ if(count>=10 && count<120){ m_ruler_count = count; update(); }}QSGNode *OrdinateItem::updatePaintNode(QSGNode *oldNode, QQuickItem::UpdatePaintNodeData *){ float max_x = (width()>30)? 20 : width(); float max_y = height(); float rulerUnit = (max_y-10)/m_ruler_count; float scal_h = max_x; float scal_m = max_x*0.8; float scal_l = max_x*0.5; int totalCount = m_ruler_count*2 + 4; QSGGeometryNode *node = 0; node = static_cast(oldNode); QSGGeometry *geometry; QSGFlatColorMaterial *material; if(!node) { node = new QSGGeometryNode; geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), totalCount); geometry->setDrawingMode(GL_LINES); geometry->setLineWidth(m_line_width); material = new QSGFlatColorMaterial; material->setColor(QColor(m_line_color)); node->setGeometry(geometry); node->setFlag(QSGNode::OwnsGeometry); node->setMaterial(material); node->setFlag(QSGNode::OwnsMaterial); } else { geometry = node->geometry(); material = static_cast(node->material()); geometry->allocate(totalCount); } QSGGeometry::Point2D *vertices = geometry->vertexDataAsPoint2D(); int max = (m_scale_vp == LeftVP)? max_x : 0; vertices[0].set(max, 0); vertices[1].set(max, max_y); float x0=0; float y0=max_y; float x; float y; int cout =1; for(int i=0;i<=m_ruler_count;i++){ int itype = (i%5==0 && i%10==0)? 0 :((i%5==0 && i%10!=0)? 1 : 2); //0:max,1:mid,2:min switch (itype) { case 0: x0 = 0; x = scal_h; y = y0; break; case 1: x0 = (m_scale_vp == LeftVP)? scal_h*0.2 : 0; x = (m_scale_vp == LeftVP)? scal_h : scal_m; y = y0; break; case 2: x0 = (m_scale_vp == LeftVP)? scal_l : 0; x = (m_scale_vp == LeftVP)? scal_h : scal_l; y = y0; break; default: break; } vertices[++cout].set(x0, y0); vertices[++cout].set(x, y); y0 = y0 - rulerUnit; } node->markDirty(QSGNode::DirtyGeometry); return node;}}

3,Canvas

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SPAlert - 原生弹出框似于Apple Music程序中的反馈,支持动画和可定制
下一篇:qml发布相关问题
相关文章

 发表评论

暂时没有评论,来抢沙发吧~