简体   繁体   中英

Connecting a private slot to a QAction in QGraphicsItem

In the next code ,i am trying to add action to the contextMenuEvent for a QGraphicsItem

but the problem is the slot "resize()" is never called

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;

}

Edit:: The header file of class "Rect"

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

我的评论是正确的:你在课堂上错过了Q_OBJECT宏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM