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. QMenubar
Forum Updated to NodeBB v4.3 + New Features

QMenubar

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.3k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    Stelian
    wrote on last edited by
    #1

    Hello!
    From the beginning I want to say I am beginner in Qt programming.
    I have the following code and I don't get any errors, but I don't understand why is not displayed nothing, not even the main window.
    Please give me a hint!
    Thank you!

    // header file
    #ifndef MENUBAR_H
    #define MENUBAR_H
    
    #include <QMainWindow>
    #include <QMenuBar>
    
    QT_BEGIN_NAMESPACE
    class QMenuBar;
    QT_END_NAMESPACE
    
    class MenuBar : public QMenuBar
    {
        Q_OBJECT
    
    public:
        MenuBar();
    
    private:
        void createWindow();
        void createMenuBar();
        QMainWindow *window;
        QMenuBar *menuBar;
    };
    
    #endif // MENUBAR_H
    
    // main file
    #include "menubar.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        MenuBar menubar;
        menubar.show();
    
        return app.exec();
    }
    
    // menubar file
    #include "menubar.h"
    #include "ui_mainwindow.h"
    
    MenuBar::MenuBar()
    {
        createWindow();
        createMenuBar();
    }
    
    void MenuBar::createWindow()
    {
        window = new QMainWindow();
        window->setWindowTitle(QString::fromUtf8("QMenuBar"));
        window->resize(400, 200);
    }
    
    void MenuBar::createMenuBar()
    {
        menuBar = new QMenuBar;
    }
    
    FlotisableF jsulmJ 2 Replies Last reply
    0
    • S Stelian

      Hello!
      From the beginning I want to say I am beginner in Qt programming.
      I have the following code and I don't get any errors, but I don't understand why is not displayed nothing, not even the main window.
      Please give me a hint!
      Thank you!

      // header file
      #ifndef MENUBAR_H
      #define MENUBAR_H
      
      #include <QMainWindow>
      #include <QMenuBar>
      
      QT_BEGIN_NAMESPACE
      class QMenuBar;
      QT_END_NAMESPACE
      
      class MenuBar : public QMenuBar
      {
          Q_OBJECT
      
      public:
          MenuBar();
      
      private:
          void createWindow();
          void createMenuBar();
          QMainWindow *window;
          QMenuBar *menuBar;
      };
      
      #endif // MENUBAR_H
      
      // main file
      #include "menubar.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication app(argc, argv);
          MenuBar menubar;
          menubar.show();
      
          return app.exec();
      }
      
      // menubar file
      #include "menubar.h"
      #include "ui_mainwindow.h"
      
      MenuBar::MenuBar()
      {
          createWindow();
          createMenuBar();
      }
      
      void MenuBar::createWindow()
      {
          window = new QMainWindow();
          window->setWindowTitle(QString::fromUtf8("QMenuBar"));
          window->resize(400, 200);
      }
      
      void MenuBar::createMenuBar()
      {
          menuBar = new QMenuBar;
      }
      
      FlotisableF Offline
      FlotisableF Offline
      Flotisable
      wrote on last edited by
      #2

      @Stelian
      Is your menubar not shown ? or the window and menuBar?

      the window and menuBar will now display if you don't call the show function explicitly or set the window and menuBar the the layout of MenuBar

      by the way, I think you will have a memory leakage since window and menuBar are not delete and have no parent

      1 Reply Last reply
      1
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Pass this parameter as parent while creating qmainwindow object inside create window function. Also I did not understand why u r inherited from menu bar etc.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        1
        • S Stelian

          Hello!
          From the beginning I want to say I am beginner in Qt programming.
          I have the following code and I don't get any errors, but I don't understand why is not displayed nothing, not even the main window.
          Please give me a hint!
          Thank you!

          // header file
          #ifndef MENUBAR_H
          #define MENUBAR_H
          
          #include <QMainWindow>
          #include <QMenuBar>
          
          QT_BEGIN_NAMESPACE
          class QMenuBar;
          QT_END_NAMESPACE
          
          class MenuBar : public QMenuBar
          {
              Q_OBJECT
          
          public:
              MenuBar();
          
          private:
              void createWindow();
              void createMenuBar();
              QMainWindow *window;
              QMenuBar *menuBar;
          };
          
          #endif // MENUBAR_H
          
          // main file
          #include "menubar.h"
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication app(argc, argv);
              MenuBar menubar;
              menubar.show();
          
              return app.exec();
          }
          
          // menubar file
          #include "menubar.h"
          #include "ui_mainwindow.h"
          
          MenuBar::MenuBar()
          {
              createWindow();
              createMenuBar();
          }
          
          void MenuBar::createWindow()
          {
              window = new QMainWindow();
              window->setWindowTitle(QString::fromUtf8("QMenuBar"));
              window->resize(400, 200);
          }
          
          void MenuBar::createMenuBar()
          {
              menuBar = new QMenuBar;
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Stelian Many questions: Why do you implement MenuBar? Why does your MenuBar contain QMainWindow (menu bar is part of a window not other way around)? Why does your MenuBar (which is already a QMenuBar) contain a QMenuBar?
          You are doing a lot of things wrongly.
          Please read this: http://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html

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

          1 Reply Last reply
          2

          • Login

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