-1

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

Suraj Jain
  • 4,463
  • 28
  • 39
amol01
  • 1,823
  • 4
  • 21
  • 35
  • filetBar = new QToolbar (this); ? – dgrat Feb 22 '14 at 15:36
  • Thanks for the answer. But still the error. – amol01 Feb 22 '14 at 15:43
  • When I for example do MainWindow::addToolbar(tr("&File")); it creates toolbar and no errors. I really do not know what is wrong here. And I partially took the code from example of qt-project MainWindow. – amol01 Feb 22 '14 at 15:44
  • Where do you initialize your QActions? – jaho Feb 22 '14 at 16:01
  • I might get it wrong, but I create Actions in createActions() function, or initializing means something else? – amol01 Feb 22 '14 at 16:05
  • In the code you posted you only initialize `openAction`. – jaho Feb 22 '14 at 16:06
  • Can you more specific? I do not know what you mean? I have other actions, but I did not implement them yet, I just want to get working and add other actions, – amol01 Feb 22 '14 at 16:09
  • Well it won't work until you either implement all actions or stop using those that are not yet implemented. See the answer below. – jaho Feb 22 '14 at 16:10

1 Answers1

1

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.
jaho
  • 4,852
  • 6
  • 40
  • 66
  • I added fileTbar = new QToolbar(this), – amol01 Feb 22 '14 at 16:19
  • And also other actions, and I guess, I have all my actions created as you have said, but I just have QIcon for them, – amol01 Feb 22 '14 at 16:22
  • still, the segmentation error at the line filetBar->addAction(openAction) – amol01 Feb 22 '14 at 16:27
  • I do not think it is about QActions, my actions are working fine, since in Menu there are working as they should. So the problem is something with QToolbar, I do not know what. – amol01 Feb 22 '14 at 16:36
  • How do you know your actions are working fine if you're not able to start the program? – jaho Feb 22 '14 at 16:46
  • When the function createToolbars() is empty literally, the programm starts, or when I just comment the function, it works, and when I have inside my function just filetBar = addToolbar(tr("&File"), the prgoram works just fine, only when I addAction to filetBar, it crashes. – amol01 Feb 22 '14 at 16:51
  • In your constructor call `createActions()` before `createToolbars()`. – jaho Feb 22 '14 at 16:51
  • You are my saver.I just did not think Qt works like this.Thank you.I do not know how things work here, like if I can vote for your answer or something? – amol01 Feb 22 '14 at 16:57
  • C++ in general works this way. You need to initialize variables before using them. You can accept the answer above by clicking a green tick. – jaho Feb 22 '14 at 17:08