Important: Please read the Qt Code of Conduct -


  • 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>
    class QMenuBar;
    class MenuBar : public QMenuBar
        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;;
        return app.exec();
    // menubar file
    #include "menubar.h"
    #include "ui_mainwindow.h"
    void MenuBar::createWindow()
        window = new QMainWindow();
        window->resize(400, 200);
    void MenuBar::createMenuBar()
        menuBar = new QMenuBar;

  • @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

  • Qt Champions 2017

    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.

  • Lifetime Qt Champion

    @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:

Log in to reply