QMenubar



  • 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;
    }
    


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



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


  • Moderators

    @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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.