在下一个代码中,我试图为 QGraphicsItem 添加操作到 contextMenuEvent
但问题是永远不会调用插槽“resize()”
void Rect::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
QMenu * menu = new QMenu();
QAction *resizeAction = menu->addAction("Resize?");
connect(resizeAction, SIGNAL(triggered()), this, SLOT(resize()));
menu->exec(event->screenPos());
delete menu;
}
编辑::类“矩形”的头文件
class Rect: public QObject,public QGraphicsItem
{
public:
enum { Type = UserType + 4 };
Rect(int x,int y,QString s,QColor myRectangleColor,
QGraphicsItem *parent = 0, QGraphicsScene *scene = 0);
Rect();
int type() const
{ return Type; }
QPainterPath shape() const;
QRectF my_rect;
virtual QRectF boundingRect() const;
virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
virtual void mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
virtual void mousePressEvent (QGraphicsSceneMouseEvent * event );
virtual void mouseReleaseEvent (QGraphicsSceneMouseEvent * event );
virtual void mouseMoveEvent(QGraphicsSceneDragDropEvent *event);
virtual void mousePressEvent(QGraphicsSceneDragDropEvent *event);
virtual bool sceneEventFilter ( QGraphicsItem * watched, QEvent * event ) ;
virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent *event);
private:
private slots:
void resize();
void MoveUpFun();
};