日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt图形视图框架--图元总结

發布時間:2023/12/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的圖元,只需要調用:

QSvgRenderer* render = new QSvgRenderer(SVG_filePath); svgItem->setSharedRenderer(render);

四、自定義圖元

所有自定義圖元都必須繼承圖元基類QGraphicsItem或其子類;

自定義圖元:添加枚舉標識、重新三個虛函數

1、標識圖元
每個圖元都有一個枚舉標識,例如路徑圖元的標識為:2
在場景操作中當檢測Type值為2時,就可以確定這個圖元是一個路徑圖元;

查看QGraphicsItem接口,可以發現Qt給開發者預留了標識:

開發者使用標識UserType即可,當有多個自定義圖元時,可以在其上累加,例如:

class LineElementItem : public QGraphicsItem { public:enum { Type = UserType + 1};LineElementItem()~LineElementItem(); }class LogicElementItem : public QGraphicsItem { public:enum { Type = UserType + 2};LogicElementItem ()~LogicElementItem (); }

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图形视图框架--图元总结的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。