Question

I am trying to build MainWindow with some toolbars and menus. So in my mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

class QAction;
class QMenu;
class QToolBar;
class QStatusBar;


class MainWindow : public QMainWindow
{
    Q_OBJECT

 public:
    MainWindow();

 protected:
    void closeEvent(QCloseEvent *event);

 private slots:
     void open();
     void save();
//void updateStatusBar();

 private:
     void createToolbars();
     void createActions();
     void createMenus();
     void createStatusBar();
 bool loadFile(const QString &fileName);
 bool saveFile(const QString &fileName);

 QToolBar *filetBar;
 QAction *openAction;
 QAction *saveAction;
 QAction *separatorAction;
 QAction *exitAction;
 QStatusBar *stsBar;
 QMenu *fileMenu;
 };

 #endif // MAINWINDOW_H

I have private variables QToolbar * filetBar; And in .cpp file, I am calling: createToolbars which is:

#include <QtGui>
#include "mainwindow.h"
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QAction>
#include <QToolBar>
#include <QMenuBar>
#include <QMenu>
#include <QStatusBar>
#include <QFileDialog>
#include <QToolButton>

//#include "mdichild.h"

 MainWindow::MainWindow()
 {
    mdiArea = new QMdiArea;
    mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    setCentralWidget(mdiArea);

    createToolbars();
    createActions();
    createMenus();
    createStatusBar();

    setWindowTitle(tr("GUI OPTIMIZER"));
    setUnifiedTitleAndToolBarOnMac(true);
    setWindowIcon(QIcon(":/images/up.jpeg"));
   }
   void MainWindow::createActions()
   {
     openAction = new QAction(QIcon(":images/open.png"), tr("&Open"), this);
     openAction->setShortcut(QKeySequence::Open);
     openAction->setStatusTip(tr("Open your gui (*.al) file"));
     connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

     saveAction = new QAction(QIcon(""), tr("&Save"), this);
saveAction->setShortcut(QKeySequence::Save);
saveAction->setStatusTip(tr("Save optimized gui"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));

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

   }

   void MainWindow::createToolbars()
   {
     filetBar = new QToolBar("File", this);
     filetBar = addToolBar(tr("&File"));
     filetBar->addAction(openAction);

   }

   void MainWindow::createMenus()
   {
      fileMenu = menuBar()->addMenu(tr("&File"));
      fileMenu->addAction(openAction);
      fileMenu->addAction(saveAction);
      separatorAction = fileMenu->addSeparator();
      fileMenu->addSeparator();
      fileMenu->addAction(exitAction);
   }

When I run, it just says The program has unexpectedly finished. When I run it with debugger is gives me

Signal name : SIGSEGV Signal meaning : Segmentation fault ad line

It show menus, and actions are working. Any idea how can I fix it? Thanks. `

Was it helpful?

Solution

You need to make sure that each of your member variables is initialized in the constructor to some sensible value before using them. This means that for each of these:

 QToolBar *filetBar;
 QAction *openAction;
 QAction *saveAction;
 QAction *separatorAction;
 QAction *exitAction;
 QStatusBar *stsBar;
 QMenu *fileMenu;

You need to have a corresponding:

filetBar = new QToolBar( /* ... */ );

In particular you are passing uninitialized QAction objects to your widgets. In your constructor, or in a separate method called from the constructor you should have a code like the following:

 openAction = new QAction("&Open", this);
 saveAction = new QAction("&Save", this);
 // etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top