vendredi 20 novembre 2020

Why am I receiving a SIGSEV from a QMenu right after launching the application?

I have a big user interface with a lot of QAction and QMenu. In order to make the QMainWindow more readable I created 2 classes:

1 class has all the most important QAction

1 class has all the most important QMenu

The problem I have is that as soon as I launch the application I receive a segmentation fault that I am not sure how to solve. However I was sure to follow all the proper procedure and as you can see in the mainwindow below. Of course in order to shrink the problem I just added the main parts:

globalmenus.h

#include <QMenu>

namespace Menu {
    class GlobalMenus : public QMenu
    {
    public:
        GlobalMenus(QObject *parent = Q_NULLPTR);

        void createMenus();

    private:
        void createFileMenu();
        void createItemMenu();
        void createHelpMenu();
        void createAboutMenu();

        QMenu *mFileMenu;
        QMenu *mItemMenu;
        QMenu *mHelpMenu;
        QMenu *mAboutMenu;

        QAction *fileNewAction;
        QAction *fileItemAction;
        QAction *fileHelpAction;
        QAction *fileAboutAction;
    };
}

globalmenus.cpp

Menu::GlobalMenus::GlobalMenus(QObject *parent)
{
    Q_UNUSED(parent)
}

void Menu::GlobalMenus::createMenus()
{
    createFileMenu();
    createItemMenu();
    createHelpMenu();
    createAboutMenu();
}

void Menu::GlobalMenus::createFileMenu()
{
    mFileMenu->addAction(fileNewAction);
}


void Menu::GlobalMenus::createItemMenu()
{
    mItemMenu->addAction(fileItemAction);
}

void Menu::GlobalMenus::createHelpMenu()
{
    mHelpMenu->addAction(fileHelpAction);
}

void Menu::GlobalMenus::createAboutMenu()
{
    mAboutMenu->addAction(fileAboutAction);
}

mainwindow.h

#include <QMainWindow>
#include "globalmenus.h"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    void createMenuBars();


private:
    Menu::GlobalMenus *allMenus;

    QMenu *mMenuFile;
    QMenu *mMenuItem;
    QMenu *mMenuHelp;
    QMenu *mMenuAbout;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"

#include <QMenu>
#include <QMenuBar>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    allMenus = new Menu::GlobalMenus;
    createMenuBars();
    allMenus->createMenus();
}

MainWindow::~MainWindow() {}

void MainWindow::createMenuBars()
{
    mMenuFile  = menuBar()->addMenu(QIcon(tr("")), tr("&File"));
    mMenuItem  = menuBar()->addMenu(QIcon(tr("")), tr("&Item"));
    mMenuHelp  = menuBar()->addMenu(QIcon(tr("")), tr("&Help"));
    mMenuAbout = menuBar()->addMenu(QIcon(tr("")), tr("&About"));
}

I am not really sure of what is going on and why it keeps not running properly.

It is also true that inside the globalmenus.h I am defining all the possible and necessary QMenu I will need. But the menuBar()->addMenu() is a member of QMainWindow.

I know for sure that if I concentrate all the QAction and QMenu inside the QMainWindow I have no problems or errors because I did it previously. However due to the high number of action/menus I wanted not to put too much components on the mainwindow.

Any idea of why that is happening that I don't see?

Thanks for pointing in the right direction for solving this problem.

Aucun commentaire:

Enregistrer un commentaire