0
votes

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.

1
seems you are not setting setMouseTracking(true);Tab

1 Answers

1
votes

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