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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

QGraphicsItem设置绘图区域和鼠标响应以及碰撞检测区域,并实现碰撞检测

發(fā)布時間:2023/12/14 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QGraphicsItem设置绘图区域和鼠标响应以及碰撞检测区域,并实现碰撞检测 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

QGraphicsItem中有兩個方法,分別用來控制QGraphicsItem的繪圖區(qū)域和碰撞檢測區(qū)域:
1.[pure virtual] QRectF QGraphicsItem::boundingRect() const
官方文檔解釋如下:
This pure virtual function defines the outer bounds of the item as a rectangle; all painting must be restricted to inside an item’s bounding rect. QGraphicsView uses this to determine whether the item requires redrawing.
Although the item’s shape can be arbitrary, the bounding rect is always rectangular, and it is unaffected by the items’ transformation.
If you want to change the item’s bounding rectangle, you must first call prepareGeometryChange(). This notifies the scene of the imminent change, so that it can update its item geometry index; otherwise, the scene will be unaware of the item’s new geometry, and the results are undefined (typically, rendering artifacts are left within the view).
Reimplement this function to let QGraphicsView determine what parts of the widget, if any, need to be redrawn.
Note: For shapes that paint an outline / stroke, it is important to include half the pen width in the bounding rect. It is not necessary to compensate for antialiasing, though.
Example:

QRectF CircleItem::boundingRect() const
{
qreal penWidth = 1;
return QRectF(-radius - penWidth / 2, -radius - penWidth / 2,
diameter + penWidth, diameter + penWidth);
}
簡單來說,通過在繼承自QGraphicsItem的子類中重寫boundingRect()方法就可以自定義item的繪圖區(qū)域,QGraphicsView 會根據boundingRect()來確定哪些區(qū)域需要重繪。

2.[virtual] QPainterPath QGraphicsItem::shape() const
Returns the shape of this item as a QPainterPath in local coordinates. The shape is used for many things, including collision detection, hit tests, and for the QGraphicsScene::items() functions.
The default implementation calls boundingRect() to return a simple rectangular shape, but subclasses can reimplement this function to return a more accurate shape for non-rectangular items. For example, a round item may choose to return an elliptic shape for better collision detection. For example:

QPainterPath RoundItem::shape() const
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}

The outline of a shape can vary depending on the width and style of the pen used when drawing. If you want to include this outline in the item’s shape, you can create a shape from the stroke using QPainterPathStroker.
This function is called by the default implementations of contains() and collidesWithPath().
簡單來說這個方法主要用來控制鼠標響應以及碰撞檢測區(qū)域。

我通過這兩個方法實現了如下碰撞檢測效果:


我的碰撞檢測代碼如下:

void myGraphicRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {QPen mPen;if(!this->scene()->collidingItems(this).isEmpty())//有重疊{mPen= QPen(Qt::red);}else{mPen= QPen(Qt::yellow);}painter->setPen(mPen);//繪制旋轉后的矩形painter->drawPolygon(m_oldRectPolygon);//繪制旋轉圓形mPen.setWidth(2);mPen.setColor(Qt::green);painter->setPen(mPen);QPointF pf = getSmallRotateRectCenter(m_oldRectPolygon[0],m_oldRectPolygon[1]);QRectF rect = QRectF(pf.x()-10,pf.y()-10,20,20);painter->drawEllipse(rect);//繪制圓形painter->drawPoint(pf);//繪制點 }

總結

以上是生活随笔為你收集整理的QGraphicsItem设置绘图区域和鼠标响应以及碰撞检测区域,并实现碰撞检测的全部內容,希望文章能夠幫你解決所遇到的問題。

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