在Qt中,通过重写`QGraphicsItem`的`contains()`函数来判断鼠标按下的点是否在已绘制的图元上。`contains()`函数用于检查给定的点是否在图元的边界框内部。
如果是widget,那么方法也是过去该图形的boundingRect,或者rect (),然后用contains判断,或者直接用坐标比大小!
```
#include <QtWidgets>
class CustomItem : public QGraphicsItem {
public:
QRectF boundingRect() const override {
return QRectF(0, 0, 100, 100);
}
void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) override {
painter->setBrush(Qt::blue);
painter->drawRect(boundingRect());
}
bool contains(const QPointF &point) const override {
return boundingRect().contains(point);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QGraphicsScene scene;