These forums look very good, its nice to have a beginners section. I've been using the official QT4 gui programming book and the spreadsheet example therein to teach myself both to use the QT designer, api and C++ having only programmed in C up until now and then a few years ago. I built an SDI spreadsheet with some functionality. I decided I want it to be MDI so used the MDIeditor example in the book to do this. The program compiles but doesn't run it crashes. I've managed to alter a few things and get an empty window up.

The gdb output is

Program received signal SIGSEGV, Segmentation fault.
0x00be5cb1 in QWidgetPrivate::init(QWidget*, QFlags<Qt::WindowType>) ()
from /usr/lib/libQtGui.so.4

using back trace I get

#0 0x00be5cb1 in QWidgetPrivate::init(QWidget*, QFlags<Qt::WindowType>) ()
from /usr/lib/libQtGui.so.4
#1 0x00be62d3 in QWidget::QWidget(QWidgetPrivate&, QWidget*, QFlags<Qt::WindowType>) () from /usr/lib/libQtGui.so.4
#2 0x00fce52e in QMdiSubWindow::QMdiSubWindow(QWidget*, QFlags<Qt::WindowType>) () from /usr/lib/libQtGui.so.4
#3 0x00fc2acf in QMdiArea::addSubWindow(QWidget*, QFlags<Qt::WindowType>) ()
from /usr/lib/libQtGui.so.4
#4 0x08050f0a in MainWindowImpl::addSpreadsheet (this=0xbffff330,
spreadsheet=0x8196398) at src/mainwindowimpl.cpp:104
#5 0x08050cb5 in MainWindowImpl::newFile (this=0xbffff330)
at src/mainwindowimpl.cpp:51
#6 0x08050e5b in MainWindowImpl::loadFiles (this=0xbffff330)
at src/mainwindowimpl.cpp:85
#7 0x0805e0c5 in MainWindowImpl::qt_metacall (this=0xbffff330,
_c=QMetaObject::InvokeMetaMethod, _id=4, _a=0xbfffebcc)
at build/moc_mainwindowimpl.cpp:89
#8 0x00964263 in QMetaObject::activate(QObject*, int, int, void**) ()
from /usr/lib/libQtCore.so.4
#9 0x00964ec2 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/libQtCore.so.4
#10 0x00969387 in ?? () from /usr/lib/libQtCore.so.4
#11 0x0096949c in ?? () from /usr/lib/libQtCore.so.4

I'm stuck I've reduced the errors from more than above by correcting some errors in my signals and slots I don't seem to have left anything out but I must be missing something.... Can anyone help. I reluctant to post the code since there is a lot of it, had to leave out spreadsheet.cpp and .h due to posting limits though the errors seem to be in the mainwindow code. I've put the linenumbers in the code below, these don't match simply since I've taken blank lines out to save space. I feel like an idiot but need help would really appreciate a simple explanation of what is wrong so I can learn from the experience. Thanks

MainWindowImpl.h
Qt Code:
  1. Q_OBJECT
  2.  
  3. public:
  4.  
  5. MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
  6.  
  7. public slots:
  8.  
  9. void printPreview();
  10. void newFile();
  11. void openFile(const QString &fileName);
  12.  
  13. private slots:
  14.  
  15.  
  16. void updateActions(); // added 11/2/09 for MDI
  17. void loadFiles();// added 11/2/09 for MDI
  18. void copy();
  19. void paste();
  20. void cut();
  21. void selectAll();
  22. void selectCurrentColumn();
  23. void selectCurrentRow();
  24. void selectFont();
  25. void goToCell();
  26.  
  27.  
  28.  
  29. private:
  30.  
  31. void createActionGroup();
  32.  
  33. QMdiArea *mdiArea;
  34. void createContextMenu();
  35. void addSpreadsheet(Spreadsheet *spreadsheet); // added 11/2/09 for MDI
  36. Spreadsheet *activeSpreadsheet(); // added 11/2/09 for MDI
  37. QMenu *windowMenu;
  38. //Spreadsheet *spreadsheet; // default constructor removed for MDI
  39.  
  40. QActionGroup *windowActionGroup;
  41. QAction *closeAction;
  42. QAction *closeAllAction;
  43. QAction *tileAction;
  44. QAction *cascadeAction;
  45. QAction *nextAction;
  46. QAction *previousAction;
  47.  
  48. };
  49.  
  50.  
  51. #endif
To copy to clipboard, switch view to plain text mode 

MainWindowImpl.cpp
Qt Code:
  1. #include <QtGui>
  2. #include "mainwindowimpl.h"
  3. #include "spreadsheet.h" // required for most menu functions
  4. #include "GoToCellDialog.h"
  5. #include "printview.h"
  6.  
  7. //
  8. MainWindowImpl::MainWindowImpl(QWidget * parent, Qt::WFlags f)
  9. : QMainWindow(parent, f)
  10. {
  11. //setAttribute(Qt::WA_DeleteOnClose); // frees memory from MDI windows
  12.  
  13. //spreadsheet = new Spreadsheet; commented out for MDI 11/2/10
  14. mdiArea = new QMdiArea;
  15. setCentralWidget(mdiArea);
  16. setupUi(this);
  17. connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)),
  18. this, SLOT(updateActions()));
  19.  
  20. createActionGroup();
  21.  
  22. QTimer::singleShot(0, this, SLOT(loadFiles()));
  23. //setCentralWidget(spreadsheet); // centre spreadsheet on form between statusbar, toolbar and
  24. //createContextMenu();
  25.  
  26. // setup signals and slots for menu actions starting with basic ones
  27. connect(actionC_opy_Ctrl_C, SIGNAL(triggered()), this, SLOT(copy()));
  28. connect(actionSelect_All_Ctrl_A, SIGNAL(triggered()), this, SLOT(selectAll()));
  29. connect(actionSelect_Co_lumn, SIGNAL(triggered()), this, SLOT(selectCurrentColumn()));
  30. connect(actionSelect_Ro_w, SIGNAL(triggered()),this, SLOT(selectCurrentRow()));
  31. connect(action_Paste_Ctrl_V, SIGNAL(triggered()), this, SLOT(paste()));
  32. connect(action_Cut_Ctrl_X, SIGNAL(triggered()), this, SLOT(cut()));
  33. connect(actionFont, SIGNAL(triggered()), this, SLOT(selectFont()));
  34. connect(actionGoto_Cell, SIGNAL(triggered()),this, SLOT(goToCell()));
  35. connect(actionPrint_Pre_view_Alt_P, SIGNAL(triggered()), this, SLOT(printPreview()));
  36. //connect(actionShow_Grid_Alt_G, SIGNAL(toggled(bool)), this, SLOT(setShowGrid(bool)));
  37.  
  38. //connect(actionPrint_S_elected_Range, SIGNAL(triggered()), this, SLOT(toHtml(const QString &plainText)));
  39. //connect(actionPrint_S_elected_Range, SIGNAL(triggered()), this, SLOT(printHtml(const QString &html)));
  40.  
  41.  
  42. connect(action_New_Ctrl_N, SIGNAL(triggered()),this, SLOT(newFile()));
  43.  
  44. }
  45. //
  46. void MainWindowImpl::newFile()
  47. {
  48. Spreadsheet *spreadsheet = new Spreadsheet;
  49. spreadsheet->newFile();
  50. addSpreadsheet(spreadsheet); // line 51
  51. }
  52. void MainWindowImpl::updateActions()
  53. {
  54. /* bool hasSpreadsheet = (activeSpreadsheet() != 0);
  55.   bool hasSelection = activeSpreadsheet()
  56.   && activeSpreadsheet()->textCursor().hasSelection();*/
  57.  
  58. /* saveAction->setEnabled(hasEditor);
  59.   saveAsAction->setEnabled(hasEditor);
  60.   cutAction->setEnabled(hasSelection);
  61.   copyAction->setEnabled(hasSelection);
  62.   pasteAction->setEnabled(hasEditor);
  63.   closeAction->setEnabled(hasEditor);
  64.   closeAllAction->setEnabled(hasEditor);
  65.   tileAction->setEnabled(hasEditor);
  66.   cascadeAction->setEnabled(hasEditor);
  67.   nextAction->setEnabled(hasEditor);
  68.   previousAction->setEnabled(hasEditor);
  69.   separatorAction->setVisible(hasEditor); */
  70.  
  71. if (activeSpreadsheet())
  72. activeSpreadsheet()->windowMenuAction()->setChecked(true);
  73. }
  74. void MainWindowImpl::loadFiles() // added 11/2/09 for MDI
  75. {
  76. QStringList args = QApplication::arguments();
  77. args.removeFirst();
  78. if (!args.isEmpty()) {
  79. foreach (QString arg, args)
  80. openFile(arg);
  81. mdiArea->cascadeSubWindows();
  82. } else {
  83. newFile(); //line 85 line 89 is case 4: loadFiles(); break; in moc file
  84. }
  85. mdiArea->activateNextSubWindow();
  86. }
  87.  
  88. void MainWindowImpl::openFile(const QString &fileName) // added 11/2/09 for MDI
  89. {
  90. Spreadsheet *spreadsheet = Spreadsheet::openFile(fileName, this);
  91. if (spreadsheet)
  92. addSpreadsheet(spreadsheet);
  93. }
  94. void MainWindowImpl::addSpreadsheet(Spreadsheet *spreadsheet) // added 11/2/09 for MDI
  95. {
  96.  
  97. QMdiSubWindow *subWindow = mdiArea->addSubWindow(spreadsheet); // line 104
  98. windowMenu->addAction(spreadsheet->windowMenuAction());
  99. windowActionGroup->addAction(spreadsheet->windowMenuAction());
  100. subWindow->show();
  101. }
  102.  
  103. Spreadsheet *MainWindowImpl::activeSpreadsheet() // added 11/2/09 for MDI
  104. {
  105. QMdiSubWindow *subWindow = mdiArea->activeSubWindow();
  106. if (subWindow)
  107. return qobject_cast<Spreadsheet *>(subWindow->widget());
  108. return 0;
  109. }
  110.  
  111. void MainWindowImpl::createActionGroup()
  112. {
  113.  
  114. windowActionGroup = new QActionGroup(this);
  115. }
  116. /*
  117. void MainWindowImpl::createContextMenu()
  118. {
  119. activeSpreadsheet()->addAction(action_Cut_Ctrl_X);//changed from spreadsheet() for MDI
  120.   activeSpreadsheet()->addAction(actionC_opy_Ctrl_C);//changed from spreadsheet() for MDI
  121.   activeSpreadsheet()->addAction(action_Paste_Ctrl_V);//changed from spreadsheet() for MDI
  122.   activeSpreadsheet()->setContextMenuPolicy(Qt::ActionsContextMenu);//changed from spreadsheet() for MDI
  123.  
  124. }
  125. */
  126. void MainWindowImpl::goToCell()
  127. {
  128. GoToCellDialog dialog(this);
  129. if (dialog.exec())
  130. {
  131. QString str = dialog.lineEdit->text().toUpper();
  132. activeSpreadsheet()->setCurrentCell(str.mid(1).toInt() - 1, //changed from spreadsheet() for MDI
  133. str[0].unicode() - 'A');
  134. }
  135. }
  136. void MainWindowImpl::printPreview()
  137. {
  138. #ifndef QT_NO_PRINTER
  139. QPrinter printer(QPrinter::ScreenResolution); // set printer to screen resolution
  140. QPrintPreviewDialog dlg(&printer);
  141. PrintView view;
  142. view.setModel(activeSpreadsheet()->model()); //changed from spreadsheet() for MDI
  143. connect(&dlg, SIGNAL(paintRequested(QPrinter *)),
  144. &view, SLOT(print(QPrinter *)));
  145. dlg.exec();
  146. #endif
  147. }
  148.  
  149. void MainWindowImpl::copy()
  150. {
  151. if (activeSpreadsheet())
  152. activeSpreadsheet()->copy();
  153. }
  154. void MainWindowImpl::paste()
  155. {
  156. if (activeSpreadsheet())
  157. activeSpreadsheet()->paste();
  158. }
  159. void MainWindowImpl::cut()
  160. {
  161. if (activeSpreadsheet())
  162. activeSpreadsheet()->cut();
  163. }
  164. void MainWindowImpl::selectAll()
  165. {
  166. if (activeSpreadsheet())
  167. activeSpreadsheet()->selectAll();
  168. }
  169.  
  170. void MainWindowImpl::selectCurrentColumn()
  171. {
  172. if (activeSpreadsheet())
  173. activeSpreadsheet()->selectCurrentColumn();
  174. }
  175.  
  176. void MainWindowImpl::selectCurrentRow()
  177. {
  178. if (activeSpreadsheet())
  179. activeSpreadsheet()->selectCurrentRow();
  180. }
To copy to clipboard, switch view to plain text mode