• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            zhonghua

            C++博客 首頁 新隨筆 聯(lián)系 聚合 管理
              72 Posts :: 1 Stories :: 4 Comments :: 0 Trackbacks
            QGraphicsItem類是視圖框架的一部分,是在一個(gè)QGraphicsScene中最基本的圖形類,它為繪制你
             
             
             
            自己的item提供了一個(gè)輕量級(jí)的窗口,包括聲明item的位置,碰撞檢測(cè),繪制重載和item之間的相
             
             
             
            互作用通過事件處理
             
            Qt提供了一系列標(biāo)準(zhǔn)的items對(duì)一些常見的圖像,非常的方便,有下面這些:
             
            QGraphicsEllipseItem  提供一個(gè)橢圓item
             
            QGraphicsLineItem     提供一條線的item
             
            QGraphicsPathItem     提供一個(gè)任意的路徑item
             
            QGraphicsPixmapItem   提供一個(gè)圖形item
             
            QGraphicsPolygonItem  提供一個(gè)多邊形item
             
            QGraphicsRectItem     提供一個(gè)矩形item
             
            QGraphicsSimpleTextItem 提供一個(gè)簡(jiǎn)單的文本item
             
            QGraphicsTextItem     提供一個(gè)文本瀏覽item
             
            item 的所有幾何位置信息都建立在本地坐標(biāo)系統(tǒng)上,item的位置,使用pos()獲得,是唯一一個(gè)不再本地item中實(shí)現(xiàn)的。它返回的是item在父親坐標(biāo)系 統(tǒng)中的位置,關(guān)于坐標(biāo)系統(tǒng)可以詳細(xì)參看 The Graphics View Coordinate System
             
             
             
            通 過調(diào)用哪個(gè)setVisible(),可以設(shè)置item是否可見,隱藏一個(gè)item同時(shí)也隱藏了他的孩子,相似的,你可以通過調(diào)用 setEnabled()來是指item是否可用。如果禁用了item,那么它所有的孩子都不可用。默認(rèn)的,items都是可見和可用的。來開關(guān)一個(gè) item是否被選擇,首先通過設(shè)置itemsSelectable flag來使能選擇。然后調(diào)用setSelect(),通常,是否可被選擇的開關(guān)時(shí)打開的
             
             
             
            寫自己的item圖形,首先應(yīng)該繼承QGraphicsItem,然后重寫他的兩個(gè)純虛公共函數(shù),

            boundingRect()和paint(),第一個(gè)函數(shù)返回繪制item大概的區(qū)域,第二個(gè)函數(shù)用來繪制item內(nèi)容
             
             
             


            boundingRect() 函數(shù)有很多用處,場(chǎng)景在boundingRect()來建立它的item的index,視圖view使用它來剪切可見的item,在重新繪制item時(shí) 候,來決定相互重疊的部分,此外,item的碰撞檢測(cè)機(jī)制也使用的boundingRect()來提供一個(gè)高效的定點(diǎn),在 collidesWithItem()更好的碰撞算法建立在調(diào)用函數(shù)shape(),shape()函數(shù)以QpainterPath類型返回item的精 準(zhǔn)的輪廓。
             
            一般的,場(chǎng)景不希望item的boundingRect()和shape()變化,除非該item被通告,如果想通過一些方法改變item的形狀,首先應(yīng)該調(diào)用QgraphicsScene()來允許場(chǎng)景QgraphicsScene來刷新它的item記錄。
             
             
             
            碰撞檢測(cè)可以通過下面兩種方法來完成
             
            1、重寫shape()函數(shù)來返回item的精準(zhǔn)輪廓,依靠默認(rèn)的collidesWithItem()來做外形交集。如果item輪廓和復(fù)雜時(shí)候,這個(gè)消耗是很大的
             
            2、重寫collidesWithItem(),提供一個(gè)自己的item和輪廓碰撞的算法
             
             
             
            Contains()函數(shù)可以調(diào)用,用來決定一個(gè)item是否包含一個(gè)點(diǎn)。這個(gè)函數(shù)也可以重寫,contains()函數(shù)默認(rèn)的方法是通過調(diào)用shape()來完成的。
             
            Items中也可以包含其他的items,也可以被別的items包含,所有的items可以有一個(gè)父親item和一串孩子items,除非一個(gè)item沒有父親,否則它的位置是在父親坐標(biāo)中,父親items遺傳他的位置和轉(zhuǎn)換給孩子item
             
            轉(zhuǎn)換
             
            QgraphicsItem 支持投射轉(zhuǎn)換,有很多方法來改變item的轉(zhuǎn)換,對(duì)于簡(jiǎn)單的轉(zhuǎn)換,可以調(diào)用函數(shù)setRotation()或者setScale(),可以傳遞一個(gè)轉(zhuǎn)換矩 陣給函數(shù)setTransform(),對(duì)于一些更復(fù)雜的轉(zhuǎn)換,可以通過調(diào)用函數(shù)setTransformations()來設(shè)置一系列組合的轉(zhuǎn)換。
             
            Item 轉(zhuǎn)換從父親到孩子進(jìn)行聚集,因此如果一個(gè)父親和孩子item都旋轉(zhuǎn)90度,那么孩子就旋轉(zhuǎn)了180度,相似的,如果父親item放大了2X倍,那么孩子 item就被方法4X倍,一個(gè)item的轉(zhuǎn)換不影響他的外觀,所有和外觀有關(guān)的函數(shù)(例如contains(),update()和所有的映射 mapping函數(shù))將會(huì)在本地坐標(biāo)中操作,更方便的,QgraphicsItem提供函數(shù)sceneTransform(),將會(huì)返回item所有的轉(zhuǎn) 換矩陣,scenePos()將會(huì)返回item在場(chǎng)景坐標(biāo)中的位置,重新設(shè)置item的矩陣,調(diào)用函數(shù)resetTransform()
             
            一 般的轉(zhuǎn)換回產(chǎn)生一個(gè)不同的結(jié)果,這取決于轉(zhuǎn)換應(yīng)用的順序,例如,如果你放大一個(gè)轉(zhuǎn)換,然后再旋轉(zhuǎn)它,可能和你先旋轉(zhuǎn)它得到的結(jié)果不一樣,你設(shè)置轉(zhuǎn)換屬性的 順序并不影響轉(zhuǎn)換的結(jié)果,(也就是仍舊會(huì)按照你的轉(zhuǎn)換命令去轉(zhuǎn)換,只是最后得到的圖形不一樣而已),QgraphicsItem經(jīng)常應(yīng)用一個(gè)合適的順序如 下:
             


            繪圖painting
             
            paint()函數(shù)被QgrapicsView類調(diào)用來繪制item的 內(nèi)容,item默認(rèn)是沒有背景或者填充顏色的。在函數(shù)中沒有被繪制的所有區(qū)域都將會(huì)發(fā)亮,可以調(diào)用update()來重繪item,可以選擇傳遞需要重繪 的矩形區(qū)域(不是必須的)。取決于item在view中是否可見,item可能會(huì)也可能不會(huì)重繪,QgraphicsItem里面沒有和 Qwidget::repaint()函數(shù)等價(jià)的
             
            item通過view來繪制,從父items開始,然后是自items,以上升的棧的 順序,可以通過調(diào)用setZValue()設(shè)置item的棧順序,通過zValue()來測(cè)試,具有低z-values的item比具有高z-value 的item先繪制,棧順序應(yīng)用于兄弟items,父items總是比子items更早繪制。
             
             
             
            排序sort
             
            所有的items都按照一個(gè)已經(jīng)聲明的,穩(wěn)定的順序來繪制,這個(gè)順序也決定了當(dāng)你在場(chǎng)景中點(diǎn)擊鼠標(biāo)時(shí)候,哪個(gè)items最先接受鼠標(biāo)的輸入。一般的,你不需要擔(dān)心排序問題,因?yàn)樗械膇tems都按照一個(gè)在場(chǎng)景中聲明的自然的順序
             


            在一個(gè)棧中,子item在父item的上面,兄弟item按照插入場(chǎng)景的順序來入棧,如果你先添加了item A ,然后是item B,然后是item C ,這樣棧中的順序從下往上就是A,B,C
             
            Drag and Drop Robot例子中展示了該robot的棧順序,軀干順序是根item(其他所有的item都是軀干item的子item或者后代item),然后是頭 item被繪制,由于它是軀干item的子item列表中的第一個(gè)item,然后是左臂膀上面的那部分item,下面那個(gè)臂膀item是上面臂膀item 的子item,所以會(huì)在和3號(hào)item同等級(jí)的兄弟item繪制完后被繪制,接著就是右邊上面的的臂膀item,就是5號(hào)item。
             
             。可以調(diào)用setZvalue()來設(shè)置一個(gè)item的相對(duì)亦另一個(gè)向上,向下或者兄弟棧順序。默認(rèn)的Z值是0,具有同樣的Z值的item會(huì)按照插入的順序來入棧。
             
            。可以調(diào)用stackBefore()來備份孩子item的列表,這可以直接更正item的順序。
             
            。如果想讓孩子item在父item的后面,也就是先繪制孩子item,然后在繪制父item,怎么辦呢?可以設(shè)置ItemStacksBehindParent屬性給這個(gè)item,利用函數(shù)setFlag();
             
            兩個(gè)兄弟item的順序也決定了他們的子item 和后代item的順序,如果一個(gè)父item的在兩個(gè)父item的后面,那么他所有的孩子items都在另一個(gè)父item的孩子items后面。
             
             
             
            事件Event
             
            QgraphicsItem從場(chǎng)景中通過sceneEvent()函數(shù)來接受事件,這個(gè)函數(shù)通過一些方便的操作分散大部分事件
             
            ContextMenuEvent()函數(shù)接受上下文菜單事件
             
            FocusInEvent()和focusOutEvent()函數(shù)接受焦點(diǎn)進(jìn)出事件
             
            hoverEnterEvent(), hoverMoveEvent(), and hoverLeaveEvent() 接受鼠標(biāo)懸浮 移動(dòng)和離開事件
             
            inputMethodEvent()函數(shù)處理輸入法事件,
             
            keyPressEvent() and keyReleaseEvent()事件處理鍵盤按下和松開事件
             
            mousePressEvent(), mouseMoveEvent(), mouseReleaseEvent(), and mouseDoubleClickEvent()處理鼠標(biāo)按下,移動(dòng),松開,雙擊事件
             
            也 可以為一些item過濾一些事件通過安裝過濾器,這個(gè)和QT一般的事件過濾器不一樣,一般的過濾器只工作在Qobject和它的子類,通過調(diào)用 installSceneEventFilter()為item安裝了過濾器后,被過濾的事件將會(huì)被虛函數(shù)sceneEventFilter().捕捉 到,可以通過調(diào)用函數(shù)removeSceneEventFilter().來去除掉事件過濾器
             
             
             
            Custom Data數(shù)據(jù)
             
            有 些時(shí)候?yàn)閕tem注冊(cè)一些數(shù)值很有用,做一個(gè)普通的item或者標(biāo)準(zhǔn)的item,可以調(diào)用setData()來為任一個(gè)item設(shè)置值,這個(gè)值使用 key-value對(duì),(key是整形,value是變種數(shù)據(jù)Qvarient)來得到item的數(shù)據(jù),通過調(diào)用data(),
             
             
             
             
             
             
             
             
             
             
             
            1、QVariant QGraphicsItem::itemChange ( GraphicsItemChange change, const QVariant & value )   [virtual protected]
             
            這個(gè)函數(shù)被QGraphicsItem調(diào)用用來標(biāo)明items的一些狀態(tài)改變了,通過重載這個(gè)函數(shù),可以對(duì)自己定義事件響應(yīng),在一些情況下,可以做一些調(diào)整。
             
            參數(shù)change是改變的那個(gè)item的改變狀態(tài)參數(shù),value是一個(gè)新的數(shù)據(jù),他的類型取決于change,
             
             
             
            change是QGraphicsItem::GraphicsItemChange的枚舉變量
             
            enum      GraphicsItemChange { ItemEnabledChange, ItemEnabledHasChanged,

             
             
            ItemMatrixChange, ItemPositionChange, ..., ItemScenePositionHasChanged }
             
            例如:
             
             QVariant Component::itemChange(GraphicsItemChange change, const QVariant &value)
             
             {
             
                 if (change == ItemPositionChange && scene()) {
             
                     // value is the new position.
             
                     QPointF newPos = value.toPointF();
             
                     QRectF rect = scene()->sceneRect();
             
                     if (!rect.contains(newPos)) {
             
                         // Keep the item inside the scene rect.
             
                         newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
             
                         newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
             
                         return newPos;
             
                     }
             
                 }
             
                 return QGraphicsItem::itemChange(change, value);
             
             }
             
            默認(rèn)的函數(shù)什么都不做,只返回value
             
            注 意:在使用這個(gè)函數(shù)時(shí)候,在函數(shù)內(nèi)部調(diào)用函數(shù)時(shí)候要小心,因?yàn)檫@可能導(dǎo)致一些意想不到的結(jié)果,例如:你不能再這個(gè)函數(shù)里面調(diào)用setPos()在 change是ItemPositionChange時(shí)候,由于setPos()函數(shù)將會(huì)再次調(diào)用 itemChange(ItemPositionChange),如此就一直循環(huán)下去了。
             
            2、void QGraphicsItem::setFlag ( GraphicsItemFlag flag, bool enabled = true )
             
            void QGraphicsItem::setFlags ( GraphicsItemFlags flags )
             
            把flags設(shè)置為item的屬性,如果item獲得了光標(biāo),但是flags沒有使能ItemsFocusable,這個(gè)item
             
             
             
            將會(huì)丟失光標(biāo),同樣的,當(dāng)item被選擇到,但是沒有使能ItemsSelectable,這個(gè)item會(huì)自動(dòng)的失去
             
             
             
            選擇。
             
            默認(rèn)的,所有的flags都是不可用的。(QGraphicsWidget 為了獲得位置變化默認(rèn)使能了
             
             
             
            ItemSendsGeometryChanges)
             
            相近的函數(shù)
             
            GraphicsItemFlags QGraphicsItem::flags () const
             
            返回item的所有使能的flags,例如,如果flags里面包含了ItemIsFocusable,這個(gè)item可以接受
             
             
             
            輸入光標(biāo)
             
            3、QPainterPath QGraphicsItem::shape () const   [virtual]
             
            以QPainterPath返回item在local坐標(biāo)中的形狀,這個(gè)形狀可以用來做很多事情,包括碰撞偵測(cè),
             
             
             
            打擊測(cè)試,還有用來 QGraphicsScene::items() 函數(shù)
             
            默認(rèn)的函數(shù)調(diào)用boundingRect()返回一個(gè)簡(jiǎn)單的矩形形狀,但是子類可以重載這個(gè)函數(shù),為非矩形
             
             
             
            的item返回一個(gè)更加精準(zhǔn)的形狀,例如一個(gè)圓形的item可以選擇返回一個(gè)橢圓形,用來獲得更好的
             
             
             
            碰撞偵測(cè)效果。代碼:
             
            QPainterPath RoundItem::shape() const
             
             {
             
                 QPainterPath path;
             
                 path.addEllipse(boundingRect());
             
                 return path;
             
             }
             
            形狀的輪廓線可以通過繪制時(shí)候的pen來變化
             
            4、QRectF QGraphicsItem::boundingRect () const   [pure virtual]
             
            這個(gè)純虛函數(shù)用矩形聲明了item的邊界輪廓,所有的繪制都必須限定在item的矩形邊框內(nèi)。
             
             
             
            QGraphicsView使用這個(gè)方法來決定item是否需要重繪
             
            盡管item的形狀可以是任意的,但是邊框一直都是矩形,不影響items的變換
             
            如果想改變items的邊框,應(yīng)該首先調(diào)用prepareGeometryChange(),這將通知場(chǎng)景scene即將發(fā)生的變化,這樣場(chǎng)景可以刷新item的位置下標(biāo)。否則,場(chǎng)景將不會(huì)察覺到item的變化,結(jié)果也未知。
             
            如果要重繪item時(shí)候,重載這個(gè)函數(shù)來讓QGraphicsView來決定item的邊界區(qū)域,
             
            注意:由于繪制邊界時(shí)候的邊界輪廓線,在這個(gè)矩形區(qū)域內(nèi)包含畫筆pen寬度的一半很重要,不需
             
             
             
            要補(bǔ)償畫圖走樣  例如
             
             QRectF CircleItem::boundingRect() const
             
             {
             
                 qreal penWidth = 1;
             
                 return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
             
                               diameter + penWidth, diameter + penWidth);
             
             }
             
             
             
            同樣的一個(gè)返回item輪廓的函數(shù)
             
            QRegion QGraphicsItem::boundingRegion ( const QTransform & itemToDeviceTransform ) const
             
            返回該item的輪廓區(qū)域,返回的區(qū)域的坐標(biāo)系統(tǒng)依賴于參數(shù)itemToDeviceTransform,如果你傳遞一個(gè)Qtransform對(duì)象作為參數(shù),那么函數(shù)將返回本地坐標(biāo)系統(tǒng)區(qū)域
             
            返回的區(qū)域是item內(nèi)容可見的一個(gè)大概的輪廓,盡管計(jì)算起來很浪費(fèi)空間和時(shí)間,但是比boundingRect()更精準(zhǔn),而且當(dāng)重繪時(shí)候,它還能避免不必要的重繪。對(duì)像線或者簡(jiǎn)單的多邊形來說非常有效。
             
            也可以調(diào)節(jié)輪廓區(qū)域的粒度通過調(diào)用setBoundingRegionGranularity(),默認(rèn)的粒度是0,這時(shí)候item的區(qū)域和輪廓矩形一樣的。
             
            itemToDeviceTransform是從item坐標(biāo)系統(tǒng)到設(shè)備坐標(biāo)系統(tǒng)的一個(gè)轉(zhuǎn)換。如果你想讓這個(gè)函數(shù)返回一個(gè)場(chǎng)景坐標(biāo)區(qū)域,可以用函數(shù)sceneTransform()作為參數(shù)。
             
             
             
            相關(guān)函數(shù)qreal QGraphicsItem::boundingRegionGranularity () const
             
            返回item的輪廓區(qū)域粒度
             
             
             
            5、void QGraphicsItem::setPos ( const QPointF & pos )
             
            在父對(duì)象坐標(biāo)系統(tǒng)中設(shè)置item的位置為pos,對(duì)沒有父對(duì)象的items,pos使用的場(chǎng)景的坐標(biāo)系統(tǒng),
             
            6、QPointF QGraphicsItem::scenePos () const
             
            返回該item在場(chǎng)景中的坐標(biāo)點(diǎn),等價(jià)于調(diào)用函數(shù)mapToScene(0, 0).
             
            7、QPointF QGraphicsItem::pos () const
             
            返回item在父坐標(biāo)系中的位置,如果沒有父坐標(biāo)系,那么返回在場(chǎng)景坐標(biāo)系中的位置,此時(shí)等價(jià)于調(diào)用scenePos (),可以直接調(diào)用scenePos ()來獲取該item在場(chǎng)景中的位置,從而忽略其父對(duì)象。
             
             
             
            8、enum QGraphicsItem::CacheMode
            設(shè)置繪圖的緩沖模式,默認(rèn)是沒有緩沖,如果使用緩沖區(qū)的話,在重繪時(shí)候,非常快
             
            設(shè)置緩沖模式使用函數(shù)QGraphicsItem::setCacheMode().
             
             
             
            9、bool QGraphicsItem::acceptDrops () const
             
            如果item能夠接受拖拽事件,就返回真,否則返回假,默認(rèn)的不能接受拖拽事件
             
             同類函數(shù)
             
            void QGraphicsItem::setAcceptDrops ( bool on )
             
            如果On是真的話,就設(shè)置item可以接受拖拽事件,否則對(duì)拖拽事件是透明的,也就是不能響應(yīng)拖拽,默認(rèn)是不響應(yīng)的。
             
            10、bool QGraphicsItem::acceptHoverEvents () const
             
            如果可以接受鼠標(biāo)懸浮在他上面的事件,就返回真,否則返回假。默認(rèn)也是不接受該事件的。
             
            同樣的用于設(shè)置是否接受該事件的函數(shù)
             
            void QGraphicsItem::setAcceptHoverEvents ( bool enabled )
             
            如果enabled為真,就接受,否則就不接受。
             
            11、Qt::MouseButtons QGraphicsItem::acceptedMouseButtons () const
             
            返回這個(gè)item所能接受的鼠標(biāo)事件的鼠標(biāo)鍵,默認(rèn)的是所有的鼠標(biāo)鍵事件都能接受。
             
            同 樣的使用函數(shù)void QGraphicsItem:: setAcceptedMouseButtons ( Qt::MouseButtons buttons )來設(shè)置接受的鼠標(biāo)鍵事件,如果想忽略某個(gè)鼠標(biāo)鍵事件,可以用setAcceptedMouseButtons(0)
             
            12、void QGraphicsItem::advance ( int phase )   [virtual]
             
            這 個(gè)虛函數(shù)被QGraphicsScene::advance()調(diào)用兩次,第一次調(diào)用,所有的items都調(diào)用,使用參數(shù)phase=0,表示場(chǎng)景中所有 的items都將advance(前進(jìn)),然后所有的items都將調(diào)用,使用參數(shù)phase=1,重寫這個(gè)函數(shù),可以來刷新你的item,如果你需要簡(jiǎn) 單的場(chǎng)景控制動(dòng)畫。
             
            默認(rèn)的該函數(shù)什么事都不做
             
            如果想寫動(dòng)畫,可以使用兩種方法,一個(gè)使用 QGraphicsItemAnimation,,或多重繼承Qobject和QgraphicsItem,然后使用 QObject::startTimer() 和 QObject::timerEvent().來使item成動(dòng)畫。
             
             
             
            13、QList<QGraphicsItem *> QGraphicsItem::childItems () const
             
            返回孩子item的一個(gè)鏈表,這些items按照棧順序排列,考慮了item的插入順序和Z-values
             
            14、QRectF QGraphicsItem::childrenBoundingRect () const
             
            返回這個(gè)item的所有子孫items的本地坐標(biāo)系統(tǒng)輪廓矩形。這個(gè)矩形包括了這個(gè)item所有的子孫item,如果這個(gè)item不含有子孫item,這個(gè)函數(shù)將返回一個(gè)空的矩形。
             
            返回的矩形不包括item本身的輪廓矩形,只返回子孫item的矩形輪廓,如果你想包含item本身的矩形區(qū)域,可以使用QRectF::operator|()把boundingRect()和hildrenBoundingRect()或起來。
             
            這個(gè)函數(shù)很復(fù)雜,它決定了返回輪廓的大小通過重復(fù)所有子孫item
             
             
             
            15、void QGraphicsItem::clearFocus ()
             
            去除item的光標(biāo),如果原來有光標(biāo)焦點(diǎn),當(dāng)焦點(diǎn)失去時(shí)候,事件focus out event會(huì)發(fā)送給這個(gè)item,提示他將會(huì)失去焦點(diǎn)。
             
            Item只有設(shè)置了ItemsFocusable屬性,或者widget設(shè)置了合適的焦點(diǎn)策略,才能接受鍵盤焦點(diǎn)。
             
             
             
             
             
            16、 bool QGraphicsItem::collidesWithItem ( const QGraphicsItem * other, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const   [virtual]
             
            如 果這個(gè)item和另一個(gè)item碰撞就返回真,mode是應(yīng)用于其他item的,默認(rèn)是Qt::IntersectsItemShape,如果其他的 item和這個(gè)item相互影響或者包含,被包含于這個(gè)item的形狀(詳見Qt::ItemSelectionMode)。
             
            默認(rèn)的函 數(shù)是就基于外形相互影響,它調(diào)用兩個(gè)item的shape()函數(shù),這將會(huì)花費(fèi)很大時(shí)間,你可以在QgraphicsItem子類中重寫這個(gè)函數(shù),提供一 個(gè)簡(jiǎn)單的算法。這樣你可以使用自己item的一些參數(shù),這樣可以提高碰撞偵測(cè)的效果。例如,兩個(gè)完全沒有轉(zhuǎn)換的圓item的碰撞可以通過對(duì)比他們的圓心位 置和半徑
             
             
             
            17、bool QGraphicsItem::collidesWithPath ( const QPainterPath & path, Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const   [virtual]
             
            如果item按照path碰撞就返回真,
             
             
             
            18、QList<QGraphicsItem *> QGraphicsItem::collidingItems ( Qt::ItemSelectionMode mode = Qt::IntersectsItemShape ) const
             
            返回和這個(gè)item相互碰撞的item的鏈表
             
            碰撞偵測(cè)模式取決于mode,
             
             
             
            19\ QGraphicsItem * QGraphicsItem::commonAncestorItem ( const QGraphicsItem * other ) const
             
            返回item最近的祖先item,如果other為0,或者沒有祖先item,就返回0
             
             
             
            20、bool QGraphicsItem::contains ( const QPointF & point ) const   [virtual]
             
            如果item包含了點(diǎn)point就返回TRUE,否則返回FALSE,最常用的是在QgraphicsView中調(diào)用,來判斷這個(gè)item是否在光標(biāo)下面,如果要重寫這個(gè)函數(shù),那就盡可能的簡(jiǎn)單。默認(rèn)的這個(gè)函數(shù)調(diào)用的shape()函數(shù)
             
             
             
            21、void QGraphicsItem::contextMenuEvent ( QGraphicsSceneContextMenuEvent * event )   [virtual protected]
             
            右鍵菜單事件,可以在子類中重寫這個(gè)函數(shù),event里面包含了事件要具體處理的數(shù)據(jù)。
             
            如果忽略這個(gè)事件,調(diào)用函數(shù)QEvent::ignore(),事件event將會(huì)被傳播到任何在該item之下的items,如果沒有item響應(yīng)這個(gè)事件,那么場(chǎng)景就忽略這個(gè)事件,傳遞給view視圖
             
            通常是響應(yīng)該事件打開一個(gè)右鍵菜單,例如
             
            void CustomItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)  {      QMenu menu;      QAction *removeAction = menu.addAction("Remove");      QAction *markAction = menu.addAction("Mark");      QAction *selectedAction = menu.exec(event->screenPos());      // ...  }
             
             
            默認(rèn)是忽略該事件。
             
             
             
             
             
            22、QVariant QGraphicsItem::data ( int key ) const
             
            以Qvariant的格式返回item中的數(shù)據(jù),當(dāng)前的item數(shù)據(jù)對(duì)存儲(chǔ)任意數(shù)據(jù)很有用,例如
             
            static const int ObjectName = 0;    QGraphicsItem *item = scene.itemAt(100, 50);  if (item->data(ObjectName).toString().isEmpty()) {      if (qgraphicsitem_cast<ButtonItem *>(item))          item->setData(ObjectName, "Button");  }
            void QGraphicsItem::setData ( int key, const QVariant & value )
             
            用來設(shè)置item的數(shù)據(jù)
             
            23、QTransform QGraphicsItem::deviceTransform ( const QTransform & viewportTransform ) const
             
            返回item設(shè)備的轉(zhuǎn)換矩陣,使用viewportTransform來從場(chǎng)景到設(shè)備影射坐標(biāo),這個(gè)矩陣可以用來從這個(gè)item本地坐標(biāo)到視圖口坐標(biāo)系統(tǒng)映射坐標(biāo)或者幾何圖形,如果要映射視圖口坐標(biāo)到本地坐標(biāo),首先應(yīng)該轉(zhuǎn)換返回的這個(gè)矩陣。
             
            QGraphicsRectItem rect;  rect.setPos(100, 100);    rect.deviceTransform(view->viewportTransform()).map(QPointF(0, 0));  // returns the item's (0, 0) point in view's viewport coordinates    rect.deviceTransform(view->viewportTransform()).inverted().map(QPointF(100, 100));  // returns view's viewport's (100, 100) coordinate in item coordinates
            這個(gè)函數(shù)是結(jié)合了到場(chǎng)景的轉(zhuǎn)換和到視圖的轉(zhuǎn)換,設(shè)備轉(zhuǎn)換可以用來對(duì)那些沒有進(jìn)行轉(zhuǎn)換坐標(biāo)的item計(jì)算碰撞。
             
            24、拖拽類函數(shù)
             
            void QGraphicsItem::dragEnterEvent ( QGraphicsSceneDragDropEvent * event )   [virtual protected]
             
            void QGraphicsItem::dragLeaveEvent ( QGraphicsSceneDragDropEvent * event )   [virtual protected]
             
            void QGraphicsItem::dragMoveEvent ( QGraphicsSceneDragDropEvent * event )   [virtual protected]
             
            void QGraphicsItem::dropEvent ( QGraphicsSceneDragDropEvent * event )   [virtual protected]
             
             
             
            25、qreal QGraphicsItem::effectiveOpacity () const
             
            返回item的有效透明度,這個(gè)數(shù)值在0.0~~1.0之間
             
            同樣的可以用函數(shù)void QGraphicsItem::setOpacity ( qreal opacity )來設(shè)置透明度,
             
            默認(rèn)的子item繼承父item的透明度
             
            同樣可以設(shè)置item的屬性QGraphicsItem::ItemIgnoresParentOpacity 來忽略父item的透明度對(duì)自己的影響
             
            設(shè)置子item的屬性QGraphicsItem::ItemIgnoresTransformations來忽略視圖轉(zhuǎn)換(放大,縮小)等操作對(duì)自己的影響,在顯示文本時(shí)候很有用。
             
             
             
            26、void QGraphicsItem::hoverEnterEvent ( QGraphicsSceneHoverEvent * event )   [virtual protected]
             
            void QGraphicsItem::hoverLeaveEvent ( QGraphicsSceneHoverEvent * event )   [virtual protected]
             
            void QGraphicsItem::hoverMoveEvent ( QGraphicsSceneHoverEvent * event )   [virtual protected]
             
            鼠標(biāo)懸浮事件
             
             
             
            27、void QGraphicsItem::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent * event )   [virtual protected]
             
            void QGraphicsItem::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )   [virtual protected]
             
            void QGraphicsItem::mousePressEvent ( QGraphicsSceneMouseEvent * event )   [virtual protected]
             
            void QGraphicsItem::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )   [virtual protected]
             
            鼠標(biāo)雙擊,移動(dòng),釋放的事件
             
             
             
             
             
             
             
            28、QPointF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QPointF & point ) const
             
            QPolygonF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QRectF & rect ) const
             
            QPainterPath QGraphicsItem::mapFromItem ( const QGraphicsItem * item, const QPainterPath & path ) const
             
            QPolygonF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, qreal x, qreal y, qreal w, qreal h ) const
             
            QPointF QGraphicsItem::mapFromItem ( const QGraphicsItem * item, qreal x, qreal y ) const
             
            QPointF QGraphicsItem::mapFromParent ( const QPointF & point ) const
             
            QPolygonF QGraphicsItem::mapFromParent ( const QRectF & rect ) const
             
            QPolygonF QGraphicsItem::mapFromParent ( const QPolygonF & polygon ) const
             
            QPainterPath QGraphicsItem::mapFromParent ( const QPainterPath & path ) const
             
            QPolygonF QGraphicsItem::mapFromParent ( qreal x, qreal y, qreal w, qreal h ) const
             
            QPointF QGraphicsItem::mapFromParent ( qreal x, qreal y ) const
             
            QPointF QGraphicsItem::mapFromScene ( const QPointF & point ) const
             
            QPolygonF QGraphicsItem::mapFromScene ( const QRectF & rect ) const
             
            QPolygonF QGraphicsItem::mapFromScene ( const QPolygonF & polygon ) const
             
            QPainterPath QGraphicsItem::mapFromScene ( const QPainterPath & path ) const
             
            QPolygonF QGraphicsItem::mapFromScene ( qreal x, qreal y, qreal w, qreal h ) const
             
            QPointF QGraphicsItem::mapFromScene ( qreal x, qreal y ) const
             
            QRectF QGraphicsItem::mapRectFromItem ( const QGraphicsItem * item, const QRectF & rect ) const
             
            QRectF QGraphicsItem::mapRectFromItem ( const QGraphicsItem * item, qreal x, qreal y, qreal w, qreal h ) const
             
            。。。。。。。。。。太多了
             
            轉(zhuǎn)換矩陣函數(shù)
             
             
             
             
             
            29、void QGraphicsItem::moveBy ( qreal dx, qreal dy )
             
            向x,y方向上移動(dòng)dx,dy的距離,等價(jià)于調(diào)用setPos(pos()+QpointF(dx,dy))
             
             
             
             
             
            30\最重要的一個(gè)函數(shù)
             
            void QGraphicsItem::paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 )   [pure virtual]
             
            這個(gè)函數(shù)通常被view調(diào)用,然后再本地坐標(biāo)系統(tǒng)中繪制item的內(nèi)容
             
             
             
            31、qreal QGraphicsItem::rotation () const
             
            返回item的旋轉(zhuǎn)度,
             
            設(shè)置旋轉(zhuǎn)度函數(shù)使用setRotation(),
             
             
             
            32、qreal QGraphicsItem::scale () const
             
            返回縮放系數(shù)
             
            設(shè)置縮放使用setScale()
             
             
             
            33、bool QGraphicsItem::sceneEvent ( QEvent * event )   [virtual protected]
             
            截獲事件,在事件沒有分發(fā)給各個(gè)具體的事件處理函數(shù)之前。
             
            相當(dāng)于bool QObject::event ( QEvent * e )   [virtual]
             
             
             
            34、void QGraphicsItem::scroll ( qreal dx, qreal dy, const QRectF & rect = QRectF() )
             
            滾動(dòng)item
             
             
             
            35、在使item能響應(yīng)一些事件之前,需要先使能這個(gè)功能,例如
             
            void QGraphicsItem::setAcceptDrops ( bool on ) 

            如果on為真,那么就使能拖拽,否則將會(huì)忽略拖拽事件。默認(rèn)是不接受的
             
            void QGraphicsItem::setAcceptHoverEvents ( bool enabled )
             
            如果on為真,使能鼠標(biāo)懸浮事件。默認(rèn)不接受該事件。
             
            void QGraphicsItem::setAcceptTouchEvents ( bool enabled )
             
            在一些支持觸摸的設(shè)備上,這個(gè)函數(shù)將使能觸摸事件。
             
             
             
            36、void QGraphicsItem::setEnabled ( bool enabled )
             
            使該item有效,如果enabled為假,則該item不能用了
             
             
             
            37、void QGraphicsItem::setGraphicsEffect ( QGraphicsEffect * effect )、
             
            設(shè)置item的效果,可以用模糊效果,印象效果,給定顏色繪制效果,透明效果等
             
            詳細(xì)的可以參見QgraphicsEffect類
             
             
             
            38、const int QGraphicsItem::Type
             
            返回該item的類型值,這個(gè)值是自己通過定義Type的值,然后使用type()虛函數(shù)來返回的,看例子
             
            class QGraphicsPathItem : public QAbstractGraphicsShapeItem  {   public:    enum { Type = 2 };      int type() const { return Type; }    ...  };
             
             
            39、T qgraphicsitem_cast ( QGraphicsItem * item )
             
            如果item是T類型的,把item轉(zhuǎn)換成T類型的item,如果失敗,則返回0
             
            如果想讓他正確的工作,需要重新實(shí)現(xiàn)函數(shù)type()來進(jìn)行判斷

            posted on 2013-05-24 11:08 米米 閱讀(4154) 評(píng)論(0)  編輯 收藏 引用 所屬分類: qt
            久久婷婷五月综合色高清| 狠狠色丁香久久综合五月| 2021国产精品午夜久久| 久久成人小视频| 久久不射电影网| 久久精品国产99国产精品亚洲| 欧美一区二区三区久久综| 久久se精品一区精品二区国产| 久久精品极品盛宴观看| 色成年激情久久综合| 亚洲国产美女精品久久久久∴ | 色偷偷久久一区二区三区| 成人免费网站久久久| 国产日韩欧美久久| 久久国产精品一国产精品金尊| 精品久久久久久无码人妻蜜桃| 欧美精品久久久久久久自慰| 午夜精品久久影院蜜桃| 国产精品免费久久久久久久久 | 日本精品久久久中文字幕| 久久久女人与动物群交毛片| 久久伊人精品一区二区三区| 成人精品一区二区久久| 777米奇久久最新地址| 久久夜色精品国产噜噜噜亚洲AV| 亚洲国产日韩综合久久精品| 久久www免费人成看国产片| 美女写真久久影院| 久久婷婷国产麻豆91天堂| 91精品国产乱码久久久久久| 亚洲αv久久久噜噜噜噜噜| 一本色综合久久| 久久久久亚洲国产| 久久精品国产色蜜蜜麻豆| 亚洲婷婷国产精品电影人久久| 久久精品国产精品亚洲| 久久久久国产日韩精品网站| 久久中文字幕视频、最近更新| 韩国三级中文字幕hd久久精品 | 国内精品九九久久精品| 亚洲综合熟女久久久30p|