Congratulations to our 2022 Qt Champions!

Open a pop-up menu

  • Hello All,

    I created a QGraphicsView with a menu, and after I selected the proper item I would like to return the selected item.

    However, when I call the show method, it is not blocked waiting the window be finished.

    How can I do it?

    Thanks all,

  • can you please post a code snippet where the problem occurs?
    From your description, it's not clear to me what you do exactly. But I can try to guess a bit:

    you call show() on the menu?

    The I should quote from the "docs:":

    bq. Warning: To make QMenu visible on the screen, exec() or popup() should be used instead of show().

    • exec() calls a modal event loop so the menu must be closed to go on.
    • popup() displays the menu and goes on in the execution logic, wiothout waiting for input.

    So perhaps, exec() is the one you were locking for?

    Enough crystal ball now :-)

  • Thanks for your reply.
    But my class does not have the exec() and popup() methods.
    It is a QGraphicsView class.

    See header below:
    class MyMenu : public QGraphicsView
    void createMenu();

    void keyPressEvent(QKeyEvent* event);
    void drawBackground(QPainter *painter, const QRectF &rect);

    MyItem *btnPage[CPAGE];
    QGraphicsScene *scene;
    QGraphicsItem *buttonParent;


  • You said in your first post that you call show() on your menu. Gerolf suggested that you use exec() or popup() on your menu instead. Could you show us the code (not header) where you actually show the menu?

  • That is not a menu. A menu is normally derived from QMenu?
    You are creating a QGraphicsView. A QGraphicsView is not a menu.

    Did you call show on the class MyMenu or on a QMenu class?

  • I am not using QMenu, I created my own Graphic Menu through QGraphicsView. So, I would like to know if it is possible to show a QGraphicsView as a popup menu.

  • Yes, of course it is, but you will have to handle the pop-up aspect of it yourself. Take a look at the QMenu sources for inspiration on how you might achieve that.

  • You must set the window flags in the constructor as popup (see QWidgetDocumentation). Opening the menu should be the same as in QMenu (look at the sources) so you should add exec / popup and implement it the same way as QMenu.

  • I used a signal/slot to communicate when the menu is selected. Thanks all.

Log in to reply