Search code examples
c++qtqwidget

QWidget::insertAction Attempt to insert null action at runtime


So, I'm trying to run my spreadsheet application in QT Creator and it compiles without errors, but when I try to run the application I get the following error QWidget::insertAction Attempt to insert null action. The problem is that I have no idea where the issue is coming from... The error is not appearing in the error section of QT Creator but in the little terminal created when running programs.

I have a createActions() function that initializes all the actions initilized in the header, maybe is coming from there?

The actions in the private part of the header look like this:

QAction *newAction;
    QAction *openAction;
    QAction *aboutQtAction;

    QAction *closeAction;
    QAction *exitAction;
    QAction *selectAllAction;
    QAction *showGridAction;
    QAction *saveAction;
    QAction *saveAsAction;
    QAction *cutAction;
    QAction *copyAction;
    QAction *pasteAction;
    QAction *deleteAction;
    QAction *selectRowAction;
    QAction *selectColumnAction;
    QAction *findAction;
    QAction *goToCellAction;
    QAction *recalculateAction;
    QAction *sortAction;
    QAction *autoRecalcAction;
    QAction *aboutAction;

Here's the function:

void MainWindow::createActions()
{
  newAction = new QAction(tr("&New"), this);
  newAction->setIcon(QIcon(":/images/avatar.jpeg"));
  newAction->setShortcut(QKeySequence::New);
  newAction->setStatusTip(tr("Create a new spreadsheet file"));
  connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));

  for (int i = 0; i < MaxRecentFiles; ++i) {
    recentFileActions[i] = new QAction(this);
    recentFileActions[i]->setVisible(false);
    connect(recentFileActions[i], SIGNAL(triggered()),this, SLOT(openRecentFile()));
  }

  closeAction = new QAction(tr("&Close"), this);
  closeAction->setShortcut(QKeySequence::Close);
  closeAction->setStatusTip(tr("Close this window"));
  connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));

  exitAction = new QAction(tr("E&xit"), this);
  exitAction->setShortcut(tr("Ctrl+Q"));
  exitAction->setStatusTip(tr("Exit the application"));
  connect(exitAction, SIGNAL(triggered()),
        qApp, SLOT(closeAllWindows()));
  selectAllAction = new QAction(tr("&All"), this);
  selectAllAction->setShortcut(QKeySequence::SelectAll);
  selectAllAction->setStatusTip(tr("Select all the cells in the "
                                    "spreadsheet"));
  connect(selectAllAction, SIGNAL(triggered()),
  spreadsheet, SLOT(selectAll()));

  showGridAction = new QAction(tr("&Show Grid"), this);
  showGridAction->setCheckable(true);
  showGridAction->setChecked(spreadsheet->showGrid());
  showGridAction->setStatusTip(tr("Show or hide the spreadsheet's "
                                  "grid"));
  connect(showGridAction, SIGNAL(toggled(bool)),
  spreadsheet, SLOT(setShowGrid(bool)));

  aboutQtAction = new QAction(tr("About &Qt"), this);
  aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
  connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
  }

These actions, after being created, are added to the menus in the createMenus() function:

void MainWindow::createMenus(){
fileMenu = menuBar()->addMenu(tr("&File"));
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);

separatorAction = fileMenu->addSeparator();
for (int i = 0; i < MaxRecentFiles; ++i)
    fileMenu->addAction(recentFileActions[i]);

fileMenu->addSeparator();
fileMenu->addAction(exitAction);
...

Someone has any idea where this error is coming from?

Thanks! Axel


Solution

  • In createActions(), you don't appear to be initializing openAction, saveAction, or saveAsAction - which you then insert into your menu. I'd guess that's the problem, unless you just didn't include the code initializing those particular actions.