Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt childForm menu into MDI menuBar



  • I have an MDI application, mainWindow with QMDIArea. The form contains the usual menus (System, Options, Window, Help). I open childWindow (also the MainWindow class) as a subWindow that have menus (eg formInvoice has an Invoice menu and actions new, edit, save ...). I need to link the childForm menu to the MDI mainForm menu, ie System, Invoice, Options ... Either automatically (I don't know how) or when one of the childForms is activated, load actions list and insert them manually into the MDI parent mainWindow menu. How to do it?

    This is how it behaves (Qt 5.14):
    MDI menu

    MDI -> opened childForm

    I need this:

    MDI -> opened childFormt

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QMdiSubWindow>
    #include <QDebug>alt tex
    #include "companies.h"
    #include "invoices.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
      Q_OBJECT
    
    public:
      MainWindow(QWidget *parent = nullptr);
      ~MainWindow();
    
    private slots:
      void on_actionCompaniesTriggered();
      void on_actionInvoicesTriggered();
      void on_mdiArea_subWindowActivated(QMdiSubWindow *arg1);
      void on_actionClose_triggered();
    
    private:
      Ui::MainWindow *ui;
      void loadSubWindow(QMainWindow *mainSubWindow);
    };
    #endif // MAINWINDOW_H
    
    #ifndef INVOICES_H
    #define INVOICES_H
    
    #include <QMainWindow>
    #include <QAction>
    #include <QMenu>
    #include <QDebug>
    
    namespace Ui {
      class Invoices;
    }
    
    class Invoices : public QMainWindow
    {
      Q_OBJECT
    
    public:
      explicit Invoices(QWidget *parent = nullptr);
      ~Invoices();
    
    private slots:
      void on_actionNewTriggered();
      void on_actionEditTriggered();
      void on_actionSaveTriggered();
      void on_actionDeleteTriggered();
      void on_actionCloseTriggered();
    
    private:
      Ui::Invoices *ui;
      QAction *action_new;
      QAction *action_edit;
      QAction *action_save;
      QAction *action_delete;
      QAction *action_close;
    
      QMenu *menuInvoices;
      void createActions();
    };
    
    #endif // INVOICES_H
    
    
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
      connect(ui->actionCompanies, SIGNAL(triggered()), this, SLOT(on_actionCompaniesTriggered()));
      connect(ui->actionInvoices, SIGNAL(triggered()), this, SLOT(on_actionInvoicesTriggered()));
    }
    
    MainWindow::~MainWindow()
    {
      delete ui;
    }
    
    void MainWindow::on_actionCompaniesTriggered(){
      loadSubWindow(new Companies(this));
    }
    
    void MainWindow::on_actionInvoicesTriggered(){
      loadSubWindow(new Invoices(this));
    }
    
    void MainWindow::loadSubWindow(QMainWindow *mainSubWindow){
      auto subWindow = ui->mdiArea->addSubWindow(mainSubWindow);
      subWindow->setAttribute(Qt::WA_DeleteOnClose);
      subWindow->setWindowTitle(mainSubWindow->windowTitle());
      subWindow->setWindowIcon(mainSubWindow->windowIcon());
      subWindow->setWindowState(Qt::WindowMaximized);
      subWindow->show();
    }
    
    void MainWindow::on_mdiArea_subWindowActivated(QMdiSubWindow *arg1)
    {
        qDebug() << arg1->windowTitle();
    }
    
    void MainWindow::on_actionClose_triggered()
    {
        close();
    }
    
    
    #include "invoices.h"
    #include "ui_invoices.h"
    
    Invoices::Invoices(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::Invoices)
    {
      ui->setupUi(this);
      createActions();
    }
    
    Invoices::~Invoices()
    {
      delete ui;
    }
    
    void Invoices::on_actionNewTriggered(){
      qDebug() << "actNew";
    }
    
    void Invoices::on_actionEditTriggered(){
      qDebug() << "actEdit";
    }
    
    void Invoices::on_actionSaveTriggered(){
      qDebug() << "actSave";
    }
    
    void Invoices::on_actionDeleteTriggered(){
      qDebug() << "actDelete";
    }
    
    void Invoices::on_actionCloseTriggered(){
      this->close();
    }
    
    void Invoices::createActions(){
      action_new = new QAction("&New", this);
      action_edit = new QAction("&Edit", this);
      action_save = new QAction("&Save", this);
      action_delete = new QAction("&Delete", this);
      action_close = new QAction("&Close", this);
      connect(action_close, SIGNAL(triggered()), this, SLOT(on_actionCloseTriggered()));
      connect(action_new, SIGNAL(triggered()), this, SLOT(on_actionNewTriggered()));
      connect(action_edit, SIGNAL(triggered()), this, SLOT(on_actionEditTriggered()));
      connect(action_save, SIGNAL(triggered()), this, SLOT(on_actionSaveTriggered()));
      connect(action_delete, SIGNAL(triggered()), this, SLOT(on_actionDeleteTriggered()));
    
      menuInvoices = new QMenu("Invoices", this);
      menuInvoices->addAction(action_new);
      menuInvoices->addAction(action_edit);
      menuInvoices->addAction(action_save);
      menuInvoices->addAction(action_delete);
      menuInvoices->addAction(action_close);
      ui->menubar->addMenu(menuInvoices);
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QMdiArea" name="mdiArea"/>
        </item>
       </layout>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>22</height>
        </rect>
       </property>
       <widget class="QMenu" name="menuSystem">
        <property name="title">
         <string>System</string>
        </property>
        <addaction name="actionCompanies"/>
        <addaction name="actionInvoices"/>
        <addaction name="actionClose"/>
        <addaction name="actionAddMenu"/>
       </widget>
       <widget class="QMenu" name="menuOptions">
        <property name="title">
         <string>Options</string>
        </property>
       </widget>
       <widget class="QMenu" name="menuWindow">
        <property name="title">
         <string>Window</string>
        </property>
       </widget>
       <widget class="QMenu" name="menuHelp">
        <property name="title">
         <string>Help</string>
        </property>
       </widget>
       <widget class="QMenu" name="menuInvoices">
        <property name="title">
         <string>Invoices</string>
        </property>
       </widget>
       <addaction name="menuSystem"/>
       <addaction name="menuInvoices"/>
       <addaction name="menuOptions"/>
       <addaction name="menuWindow"/>
       <addaction name="menuHelp"/>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
      <action name="actionCompanies">
       <property name="text">
        <string>Companies</string>
       </property>
      </action>
      <action name="actionClose">
       <property name="text">
        <string>Close</string>
       </property>
      </action>
      <action name="actionInvoices">
       <property name="text">
        <string>Invoices</string>
       </property>
      </action>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>Invoices</class>
     <widget class="QMainWindow" name="Invoices">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>800</width>
        <height>600</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Invoices</string>
      </property>
      <widget class="QWidget" name="centralwidget"/>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>800</width>
         <height>22</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

  • Lifetime Qt Champion

    Hi,

    Looks like you are on Unity. IIRC, all the menus are located in the top bar like on macOS and not on the Windows themselves.