圖形視圖框架

概述

圖形視圖為我們提供了一個支持大量自定義2D圖形項(xiàng)的管理和交互的平臺,和一個支持旋轉(zhuǎn)和縮放的視圖widget來顯示這些圖形項(xiàng).
該框架包括事件傳播架構(gòu),可以為場景中的圖形項(xiàng)提供精確的雙精度交互功能。圖形項(xiàng)可以處理鍵盤事件,鼠標(biāo)按下,移動,釋放和雙擊事件,還可以跟蹤鼠標(biāo)移動.
圖形視圖使用BSP(二叉控件分割)樹來提供快速的圖形項(xiàng)發(fā)現(xiàn),正因?yàn)槿绱?它可以高效的實(shí)時的顯示包含有百萬數(shù)量級圖形項(xiàng)的大型場景.

圖形視圖體系架構(gòu)

圖形視圖提供了基于項(xiàng)的模型-視圖編程模型,這很類似于QTableView,QTreeView,QListView之類的交互快捷類.多個視圖可以同時觀察同一個場景,場景中包含有各種幾何圖形項(xiàng).

場景(Scene)

QGraphicsScene 提供了圖形視圖場景,場景負(fù)責(zé)完成一下任務(wù):

  • 提供一個快速管理大量圖形項(xiàng)的接口.
  • 向每個圖形項(xiàng)傳遞事件
  • 管理圖形項(xiàng)的狀態(tài),例如選中,焦點(diǎn)處理等.
  • 提供無變形的渲染功能,主要用于打印.

QGraphicsScene是QGraphicsItem對象的容器.通過調(diào)用QGraphicsScene::addItem()來把QGraphicsItem對象添加到場景中.QGraphicsScene::items()返回所有場景中的圖形項(xiàng),包括點(diǎn),矩形,多邊形,適量路徑等.QGraphicsScene::itemAt()返回指定點(diǎn)處位置處在最上面的圖形項(xiàng).所有圖形項(xiàng)查找函數(shù)返回的結(jié)果都是降序堆疊的(即.第一個返回的圖形項(xiàng)在最頂層,最后一個圖形項(xiàng)在最底層).

QGraphicsScene scene;
QGraphicsRectItem* rect = scene.addRect(QRectF(0,0,100,100));
QGraphicsItem* item = scene.itemAt(50,50);   // item == rect

QGraphicsScene的事件傳播架構(gòu)調(diào)度場景事件以傳遞給圖形項(xiàng),也管理事件在圖形項(xiàng)之間的傳播.如果在場景的某個位置收到鼠標(biāo)按下事件,場景就會把此事件傳遞給在此位置的圖形項(xiàng).

QGraphicsScene還管理圖形項(xiàng)的狀態(tài),例如選擇和焦點(diǎn)狀態(tài).
調(diào)用QGraphicsScene::setSelectionArea()來選中圖形項(xiàng),
調(diào)用QGraphicsScene::selectedItems()來獲取選中的圖形項(xiàng).
調(diào)用QGraphicsScene::setFocusItem()來設(shè)置圖形項(xiàng)獲取焦點(diǎn).
調(diào)用QGraphicsScene::focusItem()來獲取當(dāng)前有焦點(diǎn)的圖形項(xiàng).

最后:
調(diào)用QGraphicsScene::render()來把場景的一部分渲染到繪圖設(shè)備中.

視圖(View)

QGraphicsView為場景內(nèi)容的可視化提供了視圖小組件(widget).可以同時把多個視圖附加到同一個場景上,這樣就可以為同一數(shù)據(jù)集提供不同的顯示形式了.視圖組件是一個滾動區(qū)域,當(dāng)場景過大時就會出現(xiàn)滾動條來方便瀏覽整個場景.為了支持OpenGL,可以通過調(diào)用QGraphicsView::setViewport()來設(shè)置一個QGLWidget作為視圖端口.

QGraphicsScene scene;
myPopulateScene(&scene);
QGraphicsView view(&scene);
view.show();

視圖接收到鍵盤和鼠標(biāo)的輸入事件后會將其轉(zhuǎn)換為場景事件(坐標(biāo)轉(zhuǎn)換為合適的場景坐標(biāo)),再有場景來分發(fā).
使用變換矩陣,QGraphicsView::transfrom(),視圖可以變換場景的坐標(biāo)系統(tǒng),以便處理高級瀏覽特性,例如縮放和旋轉(zhuǎn).為了方便,QGraphicsView提供視圖和場景坐標(biāo)系之間的轉(zhuǎn)換函數(shù):QGraphicsVeiw::mapToScene()和QGraphicsView::mapFromScene().


Image_00.png

圖形項(xiàng)(Item)

QGraphicsItem是場景中圖形項(xiàng)的基類,圖形視圖框架提供了幾個標(biāo)準(zhǔn)圖形項(xiàng),例如QGraphicsRectItem,QGraphicsEllipseItem,QGraphicsTextItem.我們當(dāng)然可以繼承QGraphicItem來自定義圖形項(xiàng).圖形項(xiàng)還支持以下特性:

  • 鼠標(biāo):按下,移動,釋放,雙擊,懸停,滾輪,上下文菜單事件.
  • 鍵盤輸入聚焦,和按鍵事件
  • 拖放事件
  • 分組:父子關(guān)系或者使用QGraphicsItemGroup.
  • 碰撞檢測
    圖形項(xiàng)與QGraphicsView一樣,存在于局部坐標(biāo)系統(tǒng)中,它提供了很多函數(shù)用于在項(xiàng)和場景之間,圖形項(xiàng)與圖形項(xiàng)之間進(jìn)行坐標(biāo)映射.另外,和QGraphicsView一樣,它通過QGraphicsItem::matrix()函數(shù)變換它的坐標(biāo)系統(tǒng),這在旋轉(zhuǎn)和縮放單個項(xiàng)時非常有用.
    圖形項(xiàng)可以包含另外的圖形項(xiàng)(子項(xiàng)),父項(xiàng)的變換被它的所有子項(xiàng)繼承.不管圖形項(xiàng)的累積變換有多少,它的所有函數(shù)(如QGraphicsItem::contains()、QGraphicsItem::boundingRect(),QGraphicsItem::collidesWith())還是在局部坐標(biāo)系統(tǒng)中操作.
    QGraphicsItem通過QGRaphicsItem::shape()函數(shù)和QGraphicsItem::collidesWith()函數(shù)支持碰撞偵測,這兩個都是虛函數(shù).通過從QGraphicsItem :: shape()返回您的項(xiàng)目的形狀作為局部坐標(biāo)QPainterPath,QGraphicsItem將為您處理所有的碰撞檢測.如果你想提供自己的碰撞偵測,則可以重新實(shí)現(xiàn)QGraphicsItem::collidesWith()函數(shù).

圖形視圖框架主要類

類名 描述
QGraphicsScene 場景:容納大量的2D圖形項(xiàng)
QGraphicsView 視圖:顯示場景中的圖形項(xiàng)的窗口小組件
QGraphicsItem 圖形項(xiàng):圖形項(xiàng)的基類
QGraphicsEllipseItem 橢圓
QGraphicsLineItem 直線
QGraphicsRectItem 矩形
QGraphicsPixmapItem 位圖
QGraphicsPathItem 路徑
QGraphicsPolygonItem 多邊形
QGraphicsSimpleTextItem 簡單文本
QGraphicsTextItem 格式化文本
QGraphicsWidget 小組件圖形項(xiàng)的基類
QGraphicsProxyWidget 把QWidget嵌入到QGraphicsScene中的代理層
QGraphicsSceneEvent 圖形視圖相關(guān)事件的基類
QGraphicsSceneDragDropEvent 拖放事件
QGraphicsSceneHoverEvent 懸浮事件
QGraphicsSceneMouseEvent 鼠標(biāo)事件
QGraphicsSceneMoveEvent 移動事件
QGraphicsSceneWheelEvent 滾輪事件
QGraphicsSceneResizeEvent 大小改變事件
QGraphicsSceneHelpEvent tooltip提示事件
QGraphicsSceneContextMenuEvent 上下文菜單事件
QGraphicsEffect 圖形效果的基類
QGraphicsAnchor 表示QGraphicsAnchorLayout中兩個圖形項(xiàng)之間的錨點(diǎn)
QGraphicsAnchorLayout 可以在圖形視圖中將窗口小部件錨在一起的布局
QGraphicsGridLayout 網(wǎng)格布局
QAbstractGraphicsShapeItem Common base for all path items
QGraphicsItemGroup Container that treats a group of items as a single item
QGraphicsObject Base class for all graphics items that require signals, slots and properties
QGraphicsLayout The base class for all layouts in Graphics View
QGraphicsLayoutItem Can be inherited to allow your custom items to be managed by layouts
QGraphicsLinearLayout Horizontal or vertical layout for managing widgets in Graphics View
QGraphicsTransform Abstract base class for building advanced transformations on QGraphicsItems
QStyleOptionGraphicsItem Used to describe the parameters needed to draw a QGraphicsItem
QGraphicsSvgItem QGraphicsItem that can be used to render the contents of SVG files

圖形視圖坐標(biāo)系

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容