QGraphisView編寫箭頭與圓指向關係

2021-10-12 10:35:31 字數 3509 閱讀 2940

效果圖

qwharrowitem類標頭檔案

#ifndef qwharrowitem_h

#define qwharrowitem_h

#include #include "qwhcircleitem.h"

class qwharrowitem : public qgraphicslineitem

; explicit qwharrowitem(qwhcircleitem *startitem, qwhcircleitem *enditem, qgraphicsitem *parent = nullptr);

~qwharrowitem();

int type() const override;

qrectf boundingrect() const override;

qpainterpath shape() const override;

protected:

void paint(qpainter *painter, const qstyleoptiongraphicsitem *option, qwidget *widget = nullptr) override;

public:

//位置更新

void updateposition();

//獲取起始item

qwhcircleitem *getstartitem();

//獲取結束item

qwhcircleitem *getenditem();

private:

qwhcircleitem *m_startitem; //起始item

qwhcircleitem *m_enditem; //結束item

qreal m_arrowsize; //箭頭大小

qcolor m_linecolor; //線條顏色

qcolor m_arrowcolor; //箭頭顏色

};#endif // qwharrowitem_h

qwharrowitem類的核心**

void qwharrowitem::paint(qpainter *painter, const qstyleoptiongraphicsitem *option, qwidget *widget)

radians += m_pi;

//獲取圓和直線的交點,箭頭繪製在交點處,不繪製在圓心(直線末端)

qpointf startpoint = line().p1() + qpointf(startitemradius * qcos(radians + m_pi), -startitemradius * qsin(radians + m_pi));

qpointf endpoint = line().p2() + qpointf(enditemradius * qcos(radians), -enditemradius * qsin(radians));

double radiusl = radians - qdegreestoradians(30.0); //箭頭左邊線段,角度可變

double radiusr = radians + qdegreestoradians(30.0); //箭頭右邊線段,角度可變

qpointf pointl = qpointf(endpoint.x() + m_arrowsize * qcos(radiusl), endpoint.y() - m_arrowsize * qsin(radiusl));

qpointf pointr = qpointf(endpoint.x() + m_arrowsize * qcos(radiusr), endpoint.y() - m_arrowsize * qsin(radiusr));

qlinef linel = qlinef(endpoint, pointl);

qlinef liner = qlinef(endpoint, pointr);

painter->setpen(qpen(m_linecolor, 2, qt::solidline, qt::roundcap, qt::roundjoin));

painter->drawline(startpoint, endpoint);

qpainterpath path;

path.moveto(endpoint);

path.lineto(pointl);

path.lineto(pointr);

painter->fillpath(path, m_arrowcolor);

}

qwhcircleitem標頭檔案

#ifndef qwhcircleitem_h

#define qwhcircleitem_h

#include class qwharrowitem;

class qwhcircleitem : public qgraphicsellipseitem

; explicit qwhcircleitem(qpointf pos, qreal radius, qgraphicsitem *parent = nullptr);

~qwhcircleitem();

int type() const override;

public:

//新增箭頭

void addarrow(qwharrowitem *arrow);

//移除箭頭

void removearrow(qwharrowitem *arrow);

//移出所有箭頭

void removearrows();

//獲取半徑

qreal radius();

protected:

//位置改變

qvariant itemchange(qgraphicsitem::graphicsitemchange change, const qvariant &value) override;

private:

qlistm_arrows; //箭頭集合

qreal m_radius; //半徑

};#endif // qwhcircleitem_h

qwhcircleitem類的核心**

void qwhcircleitem::removearrows()

}qvariant qwhcircleitem::itemchange(qgraphicsitem::graphicsitemchange change, const qvariant &value)

return value;

}

react native this繫結以及箭頭函式

對於react native繫結this,我將其歸類為以下四種方式 class testbind extends react.component del render this is a text 針對使用bind方法的繫結方式,還有一種優化方案,就是在constructor函式中繫結,這樣在效能上...

愛之箭發射(las)

小海是弓道部的成員,非常擅長射箭 love arrow shoot 今天弓道部的練習是要射一棵樹。一棵樹是乙個nn個點n 1n 1條邊的無向圖,且這棵樹的第ii個點有乙個值wiwi,wi 1,m wi 1,m 每一次小海會射中樹的一條邊,並將這條邊移除。此外,小海定義一棵樹的las值為 vi i v...

草船借箭笑談

結局1 只差一枝箭 草船上的草人中了很多箭,約有十 萬餘枝,但是仔細一數,只有九萬 九千九百九十九枝。結果周瑜依 軍法處斬諸葛亮。結局2 禁不住急拐彎 船行到曹營附近,曹軍射以亂箭,箭極多,插滿船身的一面,於是諸 葛亮把船身掉一下,船即失去平衡 側翻了,箭 船 舵手 鼓手 孔明 魯肅皆入江餵魚。結局...