QGraphicsItem鼠标在boundingRect范围内却无法触发鼠标事件
时间: 2024-01-24 09:08:25 浏览: 31
这种情况通常是因为boundingRect返回的矩形区域不包括实际显示的控件区域,你可以尝试重写QGraphicsItem的shape函数来返回一个更准确的形状,例如:
```cpp
class MyItem : public QGraphicsItem {
public:
QRectF boundingRect() const {
return QRectF(0, 0, 100, 100);
}
QPainterPath shape() const {
QPainterPath path;
path.addRect(QRectF(10, 10, 80, 80));
return path;
}
void mousePressEvent(QGraphicsSceneMouseEvent *event) {
qDebug() << "Mouse pressed!";
}
};
```
在上面的示例中,重写了shape函数来返回一个更准确的形状,这样就可以正确地触发鼠标事件了。
相关问题
QGraphicsItem::sceneBoundingRect()与QGraphicsItem::boundingRect()的区别
QGraphicsItem::boundingRect()方法返回项的本地坐标系下的边界矩形。而QGraphicsItem::sceneBoundingRect()方法返回项在场景坐标系下的边界矩形。也就是说,QGraphicsItem::boundingRect()方法返回的矩形是相对于项自身的,而QGraphicsItem::sceneBoundingRect()方法返回的矩形是相对于场景的。
通常情况下,当我们需要获取项的边界矩形时,应该使用QGraphicsItem::sceneBoundingRect()方法,因为它考虑了项的变换矩阵以及所在组的变换矩阵,可以得到准确的边界矩形。而QGraphicsItem::boundingRect()方法只返回项自身的边界矩形,不考虑变换矩阵和所在组的影响,因此不一定准确。
先执行QGraphicsItem鼠标事件接着执行QGraphicsView鼠标事件 C++
在Qt中,当鼠标点击一个QGraphicsItem时,会先触发该Item的鼠标事件,然后再传递给QGraphicsView进行处理。如果你想先处理Item的鼠标事件,可以在QGraphicsItem子类中重写mousePressEvent()、mouseReleaseEvent()、mouseMoveEvent()等事件。
如果你需要在Item的事件处理完成后再处理View的事件,可以使用QGraphicsView的viewportEvent()函数,该函数会在视口接收到事件后被调用。你可以在QGraphicsView子类中重写viewportEvent()函数,并在其中处理视口事件。
以下是一个示例代码:
```cpp
// MyItem.h
class MyItem : public QGraphicsItem
{
public:
MyItem();
QRectF boundingRect() const override;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
};
// MyItem.cpp
MyItem::MyItem()
{
setFlag(ItemIsMovable);
}
QRectF MyItem::boundingRect() const
{
return QRectF(-50, -50, 100, 100);
}
void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
painter->drawRect(boundingRect());
}
void MyItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
qDebug() << "Item mousePressEvent";
QGraphicsItem::mousePressEvent(event);
}
// MyView.h
class MyView : public QGraphicsView
{
public:
MyView(QWidget *parent = nullptr);
protected:
void viewportEvent(QEvent *event) override;
};
// MyView.cpp
MyView::MyView(QWidget *parent)
: QGraphicsView(parent)
{
}
void MyView::viewportEvent(QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress) {
qDebug() << "View mousePressEvent";
}
QGraphicsView::viewportEvent(event);
}
```
在上面的示例中,我们创建了一个MyItem类,重写了mousePressEvent()函数来处理Item的鼠标事件。我们还创建了一个MyView类,重写了viewportEvent()函数来处理View的鼠标事件。当我们在场景中点击MyItem时,会先输出"Item mousePressEvent",然后再输出"View mousePressEvent"。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)