Qt World Summit: Register Today!

How to add a Menu in a QDialog window

  • My Qt application will be more than one single window, and each window will be your own drop down menus and toolbars.

    I would like to know:
    1- What is the best:
    a) Create just one single QMainWindow for the main window and the others will be QDialog windows.
    b) Create multiple QMainWindows, one for each window

    2- If the a response is better, how can I add these 2 things in a QDialog window:

    • Drop Down menu...like File->Open
    • Toolbar...with icons such new file, save, printer, etc...

  • Lifetime Qt Champion

    There is nothing against using multiple mainwindows, however
    they are not blocking as a Dialog would/could.
    The user can at any time click on any open window.
    So if that is ok, i would go with MainWindows.

    If you want to go the Dialog way, you will have to insert some layouts to the dialog to keep both QMenu and QStatusbar where they normally are.

  • @fem_dev
    Assuming you do want independent multiple windows, not dialogs. You can create multiple QMainWindows, that just a style. When you say you want "each window will be your own drop down menus and toolbars", you can do that, but you may run out of screen space, and it can be difficult for the user to keep track of which windows he has open where.

    Have you considered whether https://doc.qt.io/qt-5/qmdiarea.html would give what you are seeking neatly? Look at the picture in https://doc.qt.io/qt-5/qtwidgets-mainwindows-mdi-example.html. In MDI you have multiple independent windows, inside an enclosing scrollable frame, and they share a single menu/toolbar on that frame. Depending on which window is currently up-front/active, the menu/toolbar changes to offer the appropriate items in the menu/toolbar. I know MDI is a bit out-of-fashion these days, but for what you are seeking it may be just what you want?

  • Thank you @mrjj ...I think that will be good to block user click in the MainWindow when the second window is enable. So QDialog will be better for my case.

    I'm newer in Qt and I don't know how to add QMenu in the layout. Should I do this in the Qt Creator Designer or by code?
    Beyond the drop-down menu (File->Open, etc...) is possible to create a UI Toolbar at the top of the QDialog window? With some icons like "New File", "Save", etc...?

    How can I do that? Is there any examples?

    I already tried this, but doesn't work:

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QToolBar *toolBar = new QToolBar();
    QAction *action1 = new QAction("Add", toolBar);
    QAction *action2 = new QAction("Del", toolBar);

    Thank you @JonB too.
    Very interesting point! I will use that in the future projects.

Log in to reply