我有一个派生自QGraphicsItem的类,基本上是这样的:
class MyRect: public QObject, public QGraphicsItem
{
Q_OBJECT
Q_INTERFACES(QGraphicsItem)
public:
explicit MyRect(QObject *parent = 0);
MyRect(QColor fillColor, float val, QString txt = "", bool isLeaf = false);
int width, height;
protected:
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
virtual QRectF boundingRect() const;
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
};
问题是mouseReleaseEvent和mousePressEvent只接受无法检测到右键单击的QGraphicsSceneMouseEvent参数。我知道有一个我可以使用的mousePressEvent(QMouseEvent * event)版本,但它似乎不适用于QGraphicsItem ....只是不能卡在这里......我很感激你的答案。
答案 0 :(得分:1)
尝试重新实现QGraphicsItem::contextMenuEvent并检查QGraphicsSceneContextMenuEvent :: Reason,如果事件是由鼠标单击引起的。