Qt图形视图框架--图元总结
生活随笔
收集整理的這篇文章主要介紹了
Qt图形视图框架--图元总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、基類QGraphicsItem
- 二、內部圖元
- 2.1、橢圓圖元--QGraphicsEllipseItem
- 2.2、線段圖元--QGraphicsLineItem
- 2.3、路徑圖元--QGraphicsPathItem
- 2.4、圖片圖元--QGraphicsPixmapItem
- 2.5、多邊形圖元--QGraphicsPolygonItem
- 2.6、矩形圖元--QGraphicsRectItem
- 2.7、簡單文本路徑圖元--QGraphicsSimpleTextItem
- 2.8、文本圖元--QGraphicsTextItem
- 三、附加圖元
- 3.1、SVG圖元--QGraphicsSvgItem
- 四、自定義圖元
一、基類QGraphicsItem
Qt圖形框架中,QGraphicsItem是圖元基類;
二、內部圖元
2.1、橢圓圖元–QGraphicsEllipseItem
2.2、線段圖元–QGraphicsLineItem
2.3、路徑圖元–QGraphicsPathItem
2.4、圖片圖元–QGraphicsPixmapItem
2.5、多邊形圖元–QGraphicsPolygonItem
2.6、矩形圖元–QGraphicsRectItem
2.7、簡單文本路徑圖元–QGraphicsSimpleTextItem
2.8、文本圖元–QGraphicsTextItem
三、附加圖元
3.1、SVG圖元–QGraphicsSvgItem
QGraphicsSvgItem是用來顯示SVG的圖元,只需要調用:
四、自定義圖元
所有自定義圖元都必須繼承圖元基類QGraphicsItem或其子類;
自定義圖元:添加枚舉標識、重新三個虛函數
1、標識圖元
每個圖元都有一個枚舉標識,例如路徑圖元的標識為:2
在場景操作中當檢測Type值為2時,就可以確定這個圖元是一個路徑圖元;
查看QGraphicsItem接口,可以發現Qt給開發者預留了標識:
開發者使用標識UserType即可,當有多個自定義圖元時,可以在其上累加,例如:
2、重寫虛函數int type() const【返回圖元類型】
class LineElementItem : public QGraphicsItem { public:enum { Type = UserType + 1};LineElementItem()~LineElementItem();int type() const{return Type;} }3、重寫虛函數QRectF boundingRect() const【設置圖元邊界】
函數boundingRect()返回一個矩形,這個矩形就是自定義圖元的區域(可視區域);
class LineElementItem : public QGraphicsItem { public:enum { Type = UserType + 1};LineElementItem()~LineElementItem();int type() const{return Type;}QRectF boundingRect() const //定義圖元為一個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 //定義圖元為一個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){// 選中時繪制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);} }總結
以上是生活随笔為你收集整理的Qt图形视图框架--图元总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SAP S4 Material Mana
- 下一篇: Paddle打比赛-古籍文档图像识别与分