Bi  Wu
Bi Wu

Reputation: 55

How to handle right mouse click event for QGraphicsItem?

I have a class derived from QGraphicsItem, which is basically like this:

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);
};

The problem is the mouseReleaseEvent and mousePressEvent only accept QGraphicsSceneMouseEvent argument which cannot detect right button click. I know there is a mousePressEvent(QMouseEvent *event) version that I can use, but it seems not work for QGraphicsItem....Just cannot get stuck here...I appreciate your answer.

Upvotes: 0

Views: 2005

Answers (1)

fjc
fjc

Reputation: 396

Try re-implementing QGraphicsItem::contextMenuEvent and checking the QGraphicsSceneContextMenuEvent::Reason for if the event was caused by a mouse click.

Upvotes: 1

Related Questions