Qt图形视图框架--图元总结
文章目錄
- 一、基類QGraphicsItem
- 二、內(nèi)部圖元
- 2.1、橢圓圖元--QGraphicsEllipseItem
- 2.2、線段圖元--QGraphicsLineItem
- 2.3、路徑圖元--QGraphicsPathItem
- 2.4、圖片圖元--QGraphicsPixmapItem
- 2.5、多邊形圖元--QGraphicsPolygonItem
- 2.6、矩形圖元--QGraphicsRectItem
- 2.7、簡(jiǎn)單文本路徑圖元--QGraphicsSimpleTextItem
- 2.8、文本圖元--QGraphicsTextItem
- 三、附加圖元
- 3.1、SVG圖元--QGraphicsSvgItem
- 四、自定義圖元
一、基類QGraphicsItem
Qt圖形框架中,QGraphicsItem是圖元基類;
二、內(nèi)部圖元
2.1、橢圓圖元–QGraphicsEllipseItem
2.2、線段圖元–QGraphicsLineItem
2.3、路徑圖元–QGraphicsPathItem
2.4、圖片圖元–QGraphicsPixmapItem
2.5、多邊形圖元–QGraphicsPolygonItem
2.6、矩形圖元–QGraphicsRectItem
2.7、簡(jiǎn)單文本路徑圖元–QGraphicsSimpleTextItem
2.8、文本圖元–QGraphicsTextItem
三、附加圖元
3.1、SVG圖元–QGraphicsSvgItem
QGraphicsSvgItem是用來(lái)顯示SVG的圖元,只需要調(diào)用:
四、自定義圖元
所有自定義圖元都必須繼承圖元基類QGraphicsItem或其子類;
自定義圖元:添加枚舉標(biāo)識(shí)、重新三個(gè)虛函數(shù)
1、標(biāo)識(shí)圖元
每個(gè)圖元都有一個(gè)枚舉標(biāo)識(shí),例如路徑圖元的標(biāo)識(shí)為:2
在場(chǎng)景操作中當(dāng)檢測(cè)Type值為2時(shí),就可以確定這個(gè)圖元是一個(gè)路徑圖元;
查看QGraphicsItem接口,可以發(fā)現(xiàn)Qt給開發(fā)者預(yù)留了標(biāo)識(shí):
開發(fā)者使用標(biāo)識(shí)UserType即可,當(dāng)有多個(gè)自定義圖元時(shí),可以在其上累加,例如:
2、重寫虛函數(shù)int type() const【返回圖元類型】
class LineElementItem : public QGraphicsItem { public:enum { Type = UserType + 1};LineElementItem()~LineElementItem();int type() const{return Type;} }3、重寫虛函數(shù)QRectF boundingRect() const【設(shè)置圖元邊界】
函數(shù)boundingRect()返回一個(gè)矩形,這個(gè)矩形就是自定義圖元的區(qū)域(可視區(qū)域);
class LineElementItem : public QGraphicsItem { public:enum { Type = UserType + 1};LineElementItem()~LineElementItem();int type() const{return Type;}QRectF boundingRect() const //定義圖元為一個(gè)128*128的矩形{qreal penwidth=1;return QRectF(0-penwidth/2,0-penwidth/2,128+penwidth,128+penwidth);} }4、重寫void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) 【繪制圖元形狀】
class LineElementItem : public QGraphicsItem { public:enum { Type = UserType + 1};LineElementItem()~LineElementItem();int type() const{return Type;}QRectF boundingRect() const //定義圖元為一個(gè)128*128的矩形{qreal penwidth=1;return QRectF(0-penwidth/2,0-penwidth/2,128+penwidth,128+penwidth);}void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){// 選中時(shí)繪制if (option->state & QStyle::State_Selected) {painter->setPen(QPen(Qt::red, 2, Qt::DashLine));painter->setBrush(Qt::NoBrush);painter->drawRect(boundingRect().adjusted(2,2,-2,-2));}QGraphicsItem::paint(painter,option,widget);} }總結(jié)
以上是生活随笔為你收集整理的Qt图形视图框架--图元总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SAP S4 Material Mana
- 下一篇: Paddle打比赛-古籍文档图像识别与分