⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Context menu long left click on QGraphicsTextItem

  • Hello,
    I try to have a context menu in Qt if we do a long press on left click on a QGraphicsItemGroup. I have try basic thing like this:
    my .h:
    class myclass: public QMainWindow
    myclass(QWidget *parent = 0);

    public slots:
    void contextMenuEvent(QContextMenuEvent *event);
    Ui::myclassClass ui;@

    my cpp:
    @ void myclass::contextMenuEvent(QContextMenuEvent *event)
    if (event)
    QMenu *menu = new QMenu;
    menu->addAction(new QAction("Action 1", this));
    menu->addAction(new QAction("Action 2", this));
    menu->addAction(new QAction("Action 3", this));

    It's work, but only with right click. And how to have this only on a QGraphicsItemGroup with a long left click ?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    One way would be to implement mousePressEvent and mouseReleaseEvent and use a QTimer for the delay.

    Also, note that you are having a memory leak here since you never delete menu.

    Hope it helps

Log in to reply