Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Multi window app with same QMenuBar
QtWS25 Last Chance

Multi window app with same QMenuBar

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 190 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kocka
    wrote on last edited by
    #1

    I would like to make 5 QMenu into QMenuBar. Each window have the same menu bar. What would be the best approach?

    first.h

    #pragma once
    
    #include <QMainWindow>
    #include <QMenuBar>
    #include "second.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class first; }
    QT_END_NAMESPACE
    
    class first : public QMainWindow {
        
        Q_OBJECT
    
    public:
        first(QWidget *parent = nullptr);
        ~first();
    
    private slots:
        void changeWindow();
    
    private:
        Ui::first *ui;
        second *sec;
    
    };
    

    first.cpp

    #include "first.h"
    #include "ui_first.h"
    
    #include <QDebug>
    
    first::first(QWidget *parent)
        : QMainWindow(parent), ui(new Ui::first) {
        ui->setupUi(this);
    
        QWidget* firstWindow = new QWidget;
    
            QMenuBar* menu = new QMenuBar(firstWindow);
    
                QMenu* menuFirst = new QMenu(menu);
                menuFirst->setObjectName("first");
                menuFirst->setTitle("first");
    
                QMenu* menuSecond = new QMenu(menu);
                menuSecond->setObjectName("second");
                menuSecond->setTitle("second");
    
                menu->addMenu(menuFirst);
                menu->addMenu(menuSecond);
    
    
        sec = new second();
    
        connect(menuFirst, &QMenu::aboutToShow, this, &first::changeWindow);
    
        
        setCentralWidget(firstWindow);
    
    }
    
    first::~first() {
        delete ui;
    }
    
    void first::changeWindow() {
    
        if(sec->isVisible()) {
            sec->hide();
            this->show();
        }
        else {
            this->hide();
            sec->show();
        }
    }
    

    With 2 window this is a good, but with 5 or 6 window this is a second-rate solution.

    One thing came to mind, create an own menuBar which inherit from QMenuBar and make a function create and that create the menuBar to each window. But maybe there is better solution.

    What would be the best to the changeWindow function?

    1 Reply Last reply
    0
    • K kocka

      @SGaist

      When I click on a menuBar element, it change the widget.

      The issue is that, if I click on a menuBar element, then I click on anywhere on the window, then I could not click on the the menuBar element. ( the menu lost the hoover )

      Qt_1.PNG
      Qt_2.PNG

      QStackedWidget* mainWidget = new QStackedWidget;
      
      	QWidget* widgetFirst = new QWidget(mainWidget);
      	QWidget* widgetSecond = new QWidget(mainWidget);
      
      	mainWidget->addWidget(widgetFirst);
      	mainWidget->addWidget(widgetSecond);
      
      
      	QMenuBar* menuBar = new QMenuBar(mainWidget);
      		menuBar->setGeometry(0,0,400,25);
      
      		QMenu* menuFirst = new QMenu(menuBar);
      		menuFirst->setObjectName("first");
      		menuFirst->setTitle("first");
      
      		QMenu* menuSecond = new QMenu(menuBar);
      		menuSecond->setObjectName("second");
      		menuSecond->setTitle("second");
      
      		menuBar->addMenu(menuFirst);
      		menuBar->addMenu(menuSecond);
      
      
      QPushButton* pushButton_1 = new QPushButton(widgetFirst);
      pushButton_1->setText("widgetFirst");
      pushButton_1->setGeometry(300,300,100,100);
      
      QPushButton* pushButton_2 = new QPushButton(widgetSecond);
      pushButton_2->setText("widgetSecond");
      pushButton_2->setGeometry(200,200,100,100);
      
      
      connect(menuFirst, &QMenu::aboutToShow, this,
      		[=]{first::changeWindow(mainWidget,widgetFirst);});
      connect(menuSecond, &QMenu::aboutToShow, this,
      		[=]{first::changeWindow(mainWidget,widgetSecond);});
      
      
      setCentralWidget(mainWidget);
      
      void first::changeWindow(QStackedWidget *w, QWidget* activate) {
          w->setCurrentWidget(activate);
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @kocka said in Multi window app with same QMenuBar:

      QMenuBar* menuBar = new QMenuBar(mainWidget);

      Why do you create a QMenuBar manually and why do you put it into QStackedWidget?
      Use https://doc.qt.io/qt-5/qmainwindow.html#menuBar to access the menu bar instead.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      3
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Looks like you should take a look at QStackedWidget.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K 1 Reply Last reply
        2
        • SGaistS SGaist

          Hi,

          Looks like you should take a look at QStackedWidget.

          K Offline
          K Offline
          kocka
          wrote on last edited by
          #3

          @SGaist

          When I click on a menuBar element, it change the widget.

          The issue is that, if I click on a menuBar element, then I click on anywhere on the window, then I could not click on the the menuBar element. ( the menu lost the hoover )

          Qt_1.PNG
          Qt_2.PNG

          QStackedWidget* mainWidget = new QStackedWidget;
          
          	QWidget* widgetFirst = new QWidget(mainWidget);
          	QWidget* widgetSecond = new QWidget(mainWidget);
          
          	mainWidget->addWidget(widgetFirst);
          	mainWidget->addWidget(widgetSecond);
          
          
          	QMenuBar* menuBar = new QMenuBar(mainWidget);
          		menuBar->setGeometry(0,0,400,25);
          
          		QMenu* menuFirst = new QMenu(menuBar);
          		menuFirst->setObjectName("first");
          		menuFirst->setTitle("first");
          
          		QMenu* menuSecond = new QMenu(menuBar);
          		menuSecond->setObjectName("second");
          		menuSecond->setTitle("second");
          
          		menuBar->addMenu(menuFirst);
          		menuBar->addMenu(menuSecond);
          
          
          QPushButton* pushButton_1 = new QPushButton(widgetFirst);
          pushButton_1->setText("widgetFirst");
          pushButton_1->setGeometry(300,300,100,100);
          
          QPushButton* pushButton_2 = new QPushButton(widgetSecond);
          pushButton_2->setText("widgetSecond");
          pushButton_2->setGeometry(200,200,100,100);
          
          
          connect(menuFirst, &QMenu::aboutToShow, this,
          		[=]{first::changeWindow(mainWidget,widgetFirst);});
          connect(menuSecond, &QMenu::aboutToShow, this,
          		[=]{first::changeWindow(mainWidget,widgetSecond);});
          
          
          setCentralWidget(mainWidget);
          
          void first::changeWindow(QStackedWidget *w, QWidget* activate) {
              w->setCurrentWidget(activate);
          }
          
          jsulmJ 1 Reply Last reply
          0
          • K kocka

            @SGaist

            When I click on a menuBar element, it change the widget.

            The issue is that, if I click on a menuBar element, then I click on anywhere on the window, then I could not click on the the menuBar element. ( the menu lost the hoover )

            Qt_1.PNG
            Qt_2.PNG

            QStackedWidget* mainWidget = new QStackedWidget;
            
            	QWidget* widgetFirst = new QWidget(mainWidget);
            	QWidget* widgetSecond = new QWidget(mainWidget);
            
            	mainWidget->addWidget(widgetFirst);
            	mainWidget->addWidget(widgetSecond);
            
            
            	QMenuBar* menuBar = new QMenuBar(mainWidget);
            		menuBar->setGeometry(0,0,400,25);
            
            		QMenu* menuFirst = new QMenu(menuBar);
            		menuFirst->setObjectName("first");
            		menuFirst->setTitle("first");
            
            		QMenu* menuSecond = new QMenu(menuBar);
            		menuSecond->setObjectName("second");
            		menuSecond->setTitle("second");
            
            		menuBar->addMenu(menuFirst);
            		menuBar->addMenu(menuSecond);
            
            
            QPushButton* pushButton_1 = new QPushButton(widgetFirst);
            pushButton_1->setText("widgetFirst");
            pushButton_1->setGeometry(300,300,100,100);
            
            QPushButton* pushButton_2 = new QPushButton(widgetSecond);
            pushButton_2->setText("widgetSecond");
            pushButton_2->setGeometry(200,200,100,100);
            
            
            connect(menuFirst, &QMenu::aboutToShow, this,
            		[=]{first::changeWindow(mainWidget,widgetFirst);});
            connect(menuSecond, &QMenu::aboutToShow, this,
            		[=]{first::changeWindow(mainWidget,widgetSecond);});
            
            
            setCentralWidget(mainWidget);
            
            void first::changeWindow(QStackedWidget *w, QWidget* activate) {
                w->setCurrentWidget(activate);
            }
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @kocka said in Multi window app with same QMenuBar:

            QMenuBar* menuBar = new QMenuBar(mainWidget);

            Why do you create a QMenuBar manually and why do you put it into QStackedWidget?
            Use https://doc.qt.io/qt-5/qmainwindow.html#menuBar to access the menu bar instead.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            K 1 Reply Last reply
            3
            • jsulmJ jsulm

              @kocka said in Multi window app with same QMenuBar:

              QMenuBar* menuBar = new QMenuBar(mainWidget);

              Why do you create a QMenuBar manually and why do you put it into QStackedWidget?
              Use https://doc.qt.io/qt-5/qmainwindow.html#menuBar to access the menu bar instead.

              K Offline
              K Offline
              kocka
              wrote on last edited by kocka
              #5

              @jsulm

              I use setMenuBar to access to the menu bar and it is working.

              setMenuBar(menuBar);
              
              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved