Page 1 of 2 12 LastLast
Results 1 to 20 of 24

Thread: begginers question

  1. #1
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default begginers question

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Try adding spreadsheet->show() in addSpreadsheet(), do you get to see a child window in the MDI then?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your reply high_flyer I really appreciate it, adding the code thus

    Qt Code:
    1. subWindow->show();
    2. spreadsheet->show();
    To copy to clipboard, switch view to plain text mode 
    gives a crash as before but a different error in gdb "Program received signal SIGSEGV, Segmentation fault.
    0x00aa1c97 in QAbstractScrollArea::viewport() const ()"

    the bt is the same

    I've posted the other relevant header file as well in case its this.... I'm sure I've not one of the objects/classes up properly, I'm beginning to get the hang of QDevelop/C++/QT designer/QT but have a long way to go.

    spreadsheet.h

    Qt Code:
    1. #ifndef SPREADSHEET_H
    2. #define SPREADSHEET_H
    3. #include <QTableWidget>
    4. #include "printview.h"
    5.  
    6. class Cell;
    7. class SpreadsheetCompare;
    8.  
    9. class Spreadsheet : public QTableWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public: // public
    14. QAction *windowMenuAction() const { return action; } //added for MDI
    15.  
    16. static Spreadsheet *open(QWidget *parent = 0);//added for MDI
    17. static Spreadsheet *openFile(const QString &fileName,//added for MDI
    18. QWidget *parent = 0);
    19. Spreadsheet(QWidget *parent = 0);
    20. void newFile();
    21. bool autoRecalculate() const { return autoRecalc; }
    22. QString currentLocation() const;
    23. QString currentFormula() const;
    24. QTableWidgetSelectionRange selectedRange() const;
    25. void clear();
    26. bool readFile(const QString &fileName);
    27. bool writeFile(const QString &fileName);
    28. void sort(const SpreadsheetCompare &compare);
    29.  
    30.  
    31. public slots: // private slots
    32.  
    33. void cut();
    34. void copy();
    35. void paste();
    36. void del();
    37. void selectCurrentRow();
    38. void selectCurrentColumn();
    39. void recalculate();
    40. void setAutoRecalculate(bool recalc);
    41. void findNext(const QString &str, Qt::CaseSensitivity cs);
    42. void findPrevious(const QString &str, Qt::CaseSensitivity cs);
    43. void selectFont();
    44. void printHtml(const QString &html);
    45. void toHtml(const QString &plainText);
    46.  
    47. signals:
    48. void modified();
    49.  
    50. private slots: //private slots
    51.  
    52. void somethingChanged();
    53.  
    54.  
    55. private: //private
    56.  
    57.  
    58. enum { MagicNumber = 0x7F51C883, RowCount = 999, ColumnCount = 26 };
    59.  
    60. Cell *cell(int row, int column) const;
    61. QString text(int row, int column) const;
    62. QString formula(int row, int column) const;
    63. void setFormula(int row, int column, const QString &formula);
    64. void setCurrentFile(const QString &fileName);
    65. QString strippedName(const QString &fullFileName);
    66. bool autoRecalc;
    67.  
    68. QString curFile;
    69. bool isUntitled;
    70. QAction *action;
    71.  
    72. };
    73.  
    74. class SpreadsheetCompare
    75. {
    76. public:
    77. bool operator()(const QStringList &row1,
    78. const QStringList &row2) const;
    79.  
    80. enum { KeyCount = 3 };
    81. int keys[KeyCount];
    82. bool ascending[KeyCount];
    83.  
    84. };
    85. #endif
    To copy to clipboard, switch view to plain text mode 

    also put function in that are causing problems referred to in spreadsheet.cpp

    Qt Code:
    1. void Spreadsheet::newFile()
    2. {
    3. static int documentNumber = 1;
    4.  
    5. curFile = tr("document%1.txt").arg(documentNumber);
    6. setWindowTitle(curFile + "[*]");
    7. action->setText(curFile);
    8. isUntitled = true;
    9. ++documentNumber;
    10. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Can you run this in debug mode and post the call stack after the crash?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thankyou for your kind reply high_flyer, sorry about the delay I have been on holiday. Here is the debug info generated by gdb.



    [Thread debugging using libthread_db enabled]
    /home/nrh1/.themes/Clearlooks-Clarity/gtk-2.0/gtkrc:49: Clearlooks configuration option "menuitemstyle" is not supported and will be ignored.
    /home/nrh1/.themes/Clearlooks-Clarity/gtk-2.0/gtkrc:50: Clearlooks configuration option "listviewitemstyle" is not supported and will be ignored.
    /home/nrh1/.themes/Clearlooks-Clarity/gtk-2.0/gtkrc:51: Clearlooks configuration option "progressbarstyle" is not supported and will be ignored.

    Program received signal SIGSEGV, Segmentation fault.
    0x00e7ac9a in QAbstractScrollArea::viewport() const ()
    from /usr/lib/libQtGui.so.4

    backtrace
    #0 0x00e7ac9a in QAbstractScrollArea::viewport() const ()
    from /usr/lib/libQtGui.so.4
    #1 0x08051098 in MainWindowImpl::addSpreadsheet (this=0xbffff330,
    spreadsheet=0x8184c48) at src/mainwindowimpl.cpp:111
    #2 0x08050fff in MainWindowImpl::newFile (this=0xbffff330)
    at src/mainwindowimpl.cpp:93
    #3 0x08050f75 in MainWindowImpl::loadFiles (this=0xbffff330)
    at src/mainwindowimpl.cpp:84
    #4 0x0805e36d in MainWindowImpl::qt_metacall (this=0xbffff330,
    _c=QMetaObject::InvokeMetaMethod, _id=4, _a=0xbfffebcc)
    at build/moc_mainwindowimpl.cpp:89
    #5 0x00321263 in QMetaObject::activate(QObject*, int, int, void**) ()
    from /usr/lib/libQtCore.so.4
    #6 0x00321ec2 in QMetaObject::activate(QObject*, QMetaObject const*, int, void**) () from /usr/lib/libQtCore.so.4
    #7 0x00326387 in ?? () from /usr/lib/libQtCore.so.4
    #8 0x0032649c in ?? () from /usr/lib/libQtCore.so.4
    #9 0x0031b3bf in QObject::event(QEvent*) () from /usr/lib/libQtCore.so.4
    #10 0x009d7f54 in QApplicationPrivate::notify_helper(QObject*, QEvent*) ()
    from /usr/lib/libQtGui.so.4
    #11 0x009df67c in QApplication::notify(QObject*, QEvent*) ()
    from /usr/lib/libQtGui.so.4
    #12 0x0030b6cb in QCoreApplication::notifyInternal(QObject*, QEvent*) ()
    from /usr/lib/libQtCore.so.4
    #13 0x003387ce in ?? () from /usr/lib/libQtCore.so.4
    #14 0x003360e0 in ?? () from /usr/lib/libQtCore.so.4
    #15 0x007aee88 in g_main_context_dispatch () from /lib/libglib-2.0.so.0
    #16 0x007b2730 in ?? () from /lib/libglib-2.0.so.0
    #17 0x007b2863 in g_main_context_iteration () from /lib/libglib-2.0.so.0
    #18 0x0033602c in QEventDispatcherGlib:rocessEvents(QFlags<QEventLoop::ProcessEventsFlag> ) () from /usr/lib/libQtCore.so.4
    #19 0x00a78be5 in ?? () from /usr/lib/libQtGui.so.4
    #20 0x00309c79 in QEventLoop:rocessEvents(QFlags<QEventLoop::ProcessEventsFlag> ) () from /usr/lib/libQtCore.so.4
    #21 0x0030a0ca in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsF lag>) ()
    from /usr/lib/libQtCore.so.4
    #22 0x0030c53f in QCoreApplication::exec() () from /usr/lib/libQtCore.so.4
    #23 0x009d7dd7 in QApplication::exec() () from /usr/lib/libQtGui.so.4
    #24 0x080570b2 in main (argc=1, argv=0xbffff514) at src/main.cpp:10

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    can you post MainWindowImpl::addSpreadsheet()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your reply cetainly can
    Qt Code:
    1. void MainWindowImpl::addSpreadsheet(Spreadsheet *spreadsheet) // added 11/2/09 for MDI
    2. {
    3. /*connect(editor, SIGNAL(copyAvailable(bool)),
    4.   cutAction, SLOT(setEnabled(bool)));
    5.   connect(editor, SIGNAL(copyAvailable(bool)),
    6.   copyAction, SLOT(setEnabled(bool))); */
    7.  
    8. QMdiSubWindow *subWindow = mdiArea->addSubWindow(spreadsheet);
    9. windowMenu->addAction(spreadsheet->windowMenuAction());
    10. windowActionGroup->addAction(spreadsheet->windowMenuAction());
    11. subWindow->show();
    12. // spreadsheet->show();
    13. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    What happesn when you try:
    Qt Code:
    1. void MainWindowImpl::addSpreadsheet(Spreadsheet *spreadsheet) // added 11/2/09 for MDI
    2. {
    3. /*connect(editor, SIGNAL(copyAvailable(bool)),
    4.   cutAction, SLOT(setEnabled(bool)));
    5.   connect(editor, SIGNAL(copyAvailable(bool)),
    6.   copyAction, SLOT(setEnabled(bool))); */
    7.  
    8. QMdiSubWindow *subWindow = mdiArea->addSubWindow(spreadsheet);
    9. windowMenu->addAction(spreadsheet->windowMenuAction());
    10. windowActionGroup->addAction(spreadsheet->windowMenuAction());
    11. spreadsheet->show();
    12. subWindow->show();
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Isn't your spreadsheet pointer being deleted when newFile exits?

  10. #10
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Sadly no change....

    Program received signal SIGSEGV, Segmentation fault.
    0x00816c9a in QAbstractScrollArea::viewport() const () from /usr/lib/libQtGui.so.4
    (gdb)

  11. #11
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Apologies, I misread your code and thought that maybe mdiArea->addSubwindow was being left hanging.

    try

    QMdiSubWindow *subWindow = mdiArea->addSubWindow(&spreadsheet);

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Ok, I think that your spreadsheet parameter has an invalid pointer.
    Can you show the code for MainWindowImpl::newFile ()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your reply....The code was above but here it is

    Qt Code:
    1. void MainWindowImpl::newFile()
    2. {
    3. Spreadsheet *spreadsheet = new Spreadsheet;
    4. spreadsheet->newFile();
    5. addSpreadsheet(spreadsheet);
    6. spreadsheet->show(); // possibly required?
    7. }
    To copy to clipboard, switch view to plain text mode 
    This calls this function in spreadsheet.cpp

    Qt Code:
    1. void Spreadsheet::newFile()
    2. {
    3. static int documentNumber = 1;
    4.  
    5. curFile = tr("document%1.txt").arg(documentNumber);
    6. setWindowTitle(curFile + "[*]");
    7. action->setText(curFile);
    8. isUntitled = true;
    9. ++documentNumber;
    10. }
    To copy to clipboard, switch view to plain text mode 

    QMdiSubWindow *subWindow = mdiArea->addSubWindow(&spreadsheet); won't compile

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Thanks for your reply....The code was above but here it is
    Sorry, I missed it.

    What does the Spreadsheet() constructor do?

    Also you can try:
    Qt Code:
    1. void MainWindowImpl::newFile()
    2. {
    3. Spreadsheet *spreadsheet = new Spreadsheet;
    4. spreadsheet->newFile();
    5. spreadsheet->show();//to see if it gets displayed or also crashes.
    6. //addSpreadsheet(spreadsheet);
    7. //spreadsheet->show(); // possibly required? not needed here if its called in addSpreadsheet()
    8. }
    To copy to clipboard, switch view to plain text mode 

    What I also noticed in spreadsheet:
    Qt Code:
    1. QAction *windowMenuAction() const { return action; } //added for MDI
    To copy to clipboard, switch view to plain text mode 
    When does 'action' gets initialized?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. #15
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your reply. I'm afraid your questions are starting to loose me....

    I've pasted the last section of remaining code spreadsheet.cpp in the hope this will throw some light on the situation. The windowMenuAction is used in the addSpreadsheet function posted previously.
    spreadsheet.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include "spreadsheet.h"
    3. #include "cell.h"
    4. #include <QTextDocument>
    5. Spreadsheet::Spreadsheet(QWidget *parent)
    6. : QTableWidget(parent)
    7. {
    8.  
    9. action = new QAction(this);
    10. action->setCheckable(true);
    11. connect(action, SIGNAL(triggered()), this, SLOT(show()));
    12. connect(action, SIGNAL(triggered()), this, SLOT(setFocus()));
    13.  
    14. isUntitled = true;
    15.  
    16. autoRecalc = true;
    17.  
    18. setItemPrototype(new Cell);
    19. setSelectionMode(ContiguousSelection);
    20.  
    21. connect(this, SIGNAL(itemChanged(QTableWidgetItem *)),
    22. this, SLOT(somethingChanged()));
    23. clear();
    24. setAttribute(Qt::WA_DeleteOnClose);
    25. }
    26. void Spreadsheet::newFile()
    27. {
    28. static int documentNumber = 1;
    29. curFile = tr("document%1.txt").arg(documentNumber);
    30. setWindowTitle(curFile + "[*]");
    31. action->setText(curFile);
    32. isUntitled = true;
    33. ++documentNumber;
    34. }
    35. QString Spreadsheet::currentLocation() const
    36. {
    37. return QChar('A' + currentColumn())
    38. + QString::number(currentRow() + 1);
    39. }
    40. QString Spreadsheet::currentFormula() const
    41. {
    42. return formula(currentRow(), currentColumn());
    43. }
    44. QTableWidgetSelectionRange Spreadsheet::selectedRange() const
    45. {
    46. QList<QTableWidgetSelectionRange> ranges = selectedRanges();
    47. if (ranges.isEmpty())
    48. return ranges.first();
    49. }
    50. void Spreadsheet::clear()
    51. {
    52. setRowCount(0);
    53. setColumnCount(0);
    54. setRowCount(RowCount);
    55. setColumnCount(ColumnCount);
    56. for (int i = 0; i < ColumnCount; ++i) {
    57. item->setText(QString(QChar('A' + i)));
    58. setHorizontalHeaderItem(i, item);
    59. }
    60. setCurrentCell(0, 0);
    61. }
    62. bool Spreadsheet::readFile(const QString &fileName)
    63. {
    64. QFile file(fileName);
    65. if (!file.open(QIODevice::ReadOnly)) {
    66. QMessageBox::warning(this, tr("Spreadsheet"),
    67. tr("Cannot read file %1:\n%2.")
    68. .arg(file.fileName())
    69. .arg(file.errorString()));
    70. return false;
    71. }
    72.  
    73. QDataStream in(&file);
    74. in.setVersion(QDataStream::Qt_4_3);
    75.  
    76. quint32 magic;
    77. in >> magic;
    78. if (magic != MagicNumber) {
    79. QMessageBox::warning(this, tr("Spreadsheet"),
    80. tr("The file is not a Spreadsheet file."));
    81. return false;
    82. }
    83.  
    84. clear();
    85.  
    86. quint16 row;
    87. quint16 column;
    88. QString str;
    89.  
    90. QApplication::setOverrideCursor(Qt::WaitCursor);
    91. while (!in.atEnd()) {
    92. in >> row >> column >> str;
    93. setFormula(row, column, str);
    94. }
    95. QApplication::restoreOverrideCursor();
    96. return true;
    97. }
    98.  
    99. bool Spreadsheet::writeFile(const QString &fileName)
    100. {
    101. QFile file(fileName);
    102. if (!file.open(QIODevice::WriteOnly)) {
    103. QMessageBox::warning(this, tr("Spreadsheet"),
    104. tr("Cannot write file %1:\n%2.")
    105. .arg(file.fileName())
    106. .arg(file.errorString()));
    107. return false;
    108. }
    109.  
    110. QDataStream out(&file);
    111. out.setVersion(QDataStream::Qt_4_3);
    112.  
    113. out << quint32(MagicNumber);
    114.  
    115. QApplication::setOverrideCursor(Qt::WaitCursor);
    116. for (int row = 0; row < RowCount; ++row) {
    117. for (int column = 0; column < ColumnCount; ++column) {
    118. QString str = formula(row, column);
    119. if (!str.isEmpty())
    120. out << quint16(row) << quint16(column) << str;
    121. }
    122. }
    123. QApplication::restoreOverrideCursor();
    124. return true;
    125. }
    126. void Spreadsheet::sort(const SpreadsheetCompare &compare)
    127. {
    128. QList<QStringList> rows;
    129. QTableWidgetSelectionRange range = selectedRange();
    130. int i;
    131.  
    132. for (i = 0; i < range.rowCount(); ++i) {
    133. for (int j = 0; j < range.columnCount(); ++j)
    134. row.append(formula(range.topRow() + i,
    135. range.leftColumn() + j));
    136. rows.append(row);
    137. }
    138.  
    139. qStableSort(rows.begin(), rows.end(), compare);
    140.  
    141. for (i = 0; i < range.rowCount(); ++i) {
    142. for (int j = 0; j < range.columnCount(); ++j)
    143. setFormula(range.topRow() + i, range.leftColumn() + j,
    144. rows[i][j]);
    145. }
    146.  
    147. clearSelection();
    148. somethingChanged();
    149. }
    150. void Spreadsheet::cut()
    151. {
    152. copy();
    153. del();
    154. }
    155. void Spreadsheet::copy()
    156. {
    157. QTableWidgetSelectionRange range = selectedRange();
    158. QString str;
    159.  
    160. for (int i = 0; i < range.rowCount(); ++i) {
    161. if (i > 0)
    162. str += "\n";
    163. for (int j = 0; j < range.columnCount(); ++j) {
    164. if (j > 0)
    165. str += "\t";
    166. str += formula(range.topRow() + i, range.leftColumn() + j);
    167. }
    168. }
    169. QApplication::clipboard()->setText(str);
    170. }
    171. void Spreadsheet::paste()
    172. {
    173. QTableWidgetSelectionRange range = selectedRange();
    174. QString str = QApplication::clipboard()->text();
    175. QStringList rows = str.split('\n');
    176. int numRows = rows.count();
    177. int numColumns = rows.first().count('\t') + 1;
    178.  
    179. if (range.rowCount() * range.columnCount() != 1
    180. && (range.rowCount() != numRows
    181. || range.columnCount() != numColumns)) {
    182. QMessageBox::information(this, tr("Spreadsheet"),
    183. tr("The information cannot be pasted because the copy "
    184. "and paste areas aren't the same size."));
    185. return;
    186. }
    187.  
    188. for (int i = 0; i < numRows; ++i) {
    189. QStringList columns = rows[i].split('\t');
    190. for (int j = 0; j < numColumns; ++j) {
    191. int row = range.topRow() + i;
    192. int column = range.leftColumn() + j;
    193. if (row < RowCount && column < ColumnCount)
    194. setFormula(row, column, columns[j]);
    195. }
    196. }
    197. somethingChanged();
    198. }
    199. void Spreadsheet::del()
    200. {
    201. QList<QTableWidgetItem *> items = selectedItems();
    202. if (!items.isEmpty()) {
    203. foreach (QTableWidgetItem *item, items)
    204. delete item;
    205. somethingChanged();
    206. }
    207. }
    208. void Spreadsheet::selectCurrentRow()
    209. {
    210. selectRow(currentRow());
    211. }
    212.  
    213. void Spreadsheet::selectCurrentColumn()
    214. {
    215. selectColumn(currentColumn());
    216. }
    217.  
    218. void Spreadsheet::recalculate()
    219. {
    220. for (int row = 0; row < RowCount; ++row) {
    221. for (int column = 0; column < ColumnCount; ++column) {
    222. if (cell(row, column))
    223. cell(row, column)->setDirty();
    224. }
    225. }
    226. viewport()->update();
    227. }
    228. void Spreadsheet::setAutoRecalculate(bool recalc)
    229. {
    230. autoRecalc = recalc;
    231. if (autoRecalc)
    232. recalculate();
    233. }
    234.  
    235.  
    236. void Spreadsheet::somethingChanged()
    237. {
    238. if (autoRecalc)
    239. recalculate();
    240. emit modified();
    241. }
    242.  
    243. Cell *Spreadsheet::cell(int row, int column) const
    244. {
    245. return static_cast<Cell *>(item(row, column));
    246. }
    247.  
    248. void Spreadsheet::setFormula(int row, int column,
    249. const QString &formula)
    250. {
    251. Cell *c = cell(row, column);
    252. if (!c) {
    253. c = new Cell;
    254. setItem(row, column, c);
    255. }
    256. c->setFormula(formula);
    257. }
    258.  
    259. QString Spreadsheet::formula(int row, int column) const
    260. {
    261. Cell *c = cell(row, column);
    262. if (c) {
    263. return c->formula();
    264. } else {
    265. return "";
    266. }
    267. }
    268.  
    269. QString Spreadsheet::text(int row, int column) const
    270. {
    271. Cell *c = cell(row, column);
    272. if (c) {
    273. return c->text();
    274. } else {
    275. return "";
    276. }
    277. }
    278.  
    279. bool SpreadsheetCompare::operator()(const QStringList &row1,
    280. const QStringList &row2) const
    281. {
    282. for (int i = 0; i < KeyCount; ++i) {
    283. int column = keys[i];
    284. if (column != -1) {
    285. if (row1[column] != row2[column]) {
    286. if (ascending[i]) {
    287. return row1[column] < row2[column];
    288. } else {
    289. return row1[column] > row2[column];
    290. }
    291. }
    292. }
    293. }
    294. return false;
    295. }
    296.  
    297.  
    298. void Spreadsheet::selectFont()
    299. {
    300. QList<QTableWidgetItem *> items = selectedItems();
    301. if (items.isEmpty())
    302. return;
    303.  
    304.  
    305. bool ok = false;
    306. QFont fnt = QFontDialog::getFont(&ok, font(), this);
    307.  
    308. if (!ok)
    309. return;
    310. foreach (QTableWidgetItem *item, items)
    311. if(item)
    312. item->setFont(fnt);
    313. somethingChanged();
    314.  
    315. }
    316.  
    317. void::Spreadsheet::toHtml(const QString &plainText)
    318. {
    319. QTableWidgetSelectionRange range = selectedRange();
    320. QString html= Qt::escape(plainText);
    321.  
    322. for (int i = 0; i < range.rowCount(); ++i) {
    323. if (i > 0)
    324. html.replace("\n", "\n<tr><td>");
    325. for (int j = 0; j < range.columnCount(); ++j) {
    326. if (j > 0)
    327. html.replace("\t", "\t<tr><td>");
    328.  
    329. html.prepend("<table>\n<tr><td>");
    330. html.append("\n</table>");
    331. }
    332. }
    333. printHtml(html);
    334.  
    335. }
    336. }
    337. Spreadsheet *Spreadsheet::openFile(const QString &fileName, QWidget *parent)
    338. {
    339. Spreadsheet *spreadsheet = new Spreadsheet(parent);
    340. if (spreadsheet->readFile(fileName))
    341. {
    342. spreadsheet->setCurrentFile(fileName);
    343. return spreadsheet;
    344. } else
    345. {
    346. delete spreadsheet;
    347. return 0;
    348. }
    349. }
    350. void Spreadsheet::setCurrentFile(const QString &fileName)
    351. {
    352. curFile = fileName;
    353. isUntitled = false;
    354. action->setText(strippedName(curFile));
    355. //document()->setModified(false);
    356. setWindowTitle(strippedName(curFile) + "[*]");
    357. setWindowModified(false);
    358. }
    To copy to clipboard, switch view to plain text mode 

  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    its really hard to do help you this way.
    Set a break point in addSpreadSheet() first line, and step through until it crashes.
    Then you know which line crashed.
    Then examine the variables, and I guess you will see a bad pointer there.
    It looks like you have an initialization problem somewhere in your code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  17. #17
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your reply again. This has just got very strange. The results I get depend on what I debug with....
    If I use KDbg and just run program from within it, it works perfectly! I cannot see all the source code to set a breakpoint it only shows the first few lines.
    If I use gdb at the command line and set the break at the function you suggested (void MainWindowImpl::addSpreadsheet(Spreadsheet *spreadsheet)) it works again perfectly!

    If I use QDevelop and set the breakpoint at void MainWindowImpl::addSpreadsheet(Spreadsheet *spreadsheet) it comes up with this message "
    Scope for 115:
    Symbol subWindow is a variable with complex or multiple locations (DWARF2), length 4.
    Symbol this is a variable with complex or multiple locations (DWARF2), length 4.
    Symbol spreadsheet is a variable with complex or multiple locations (DWARF2), length 4.
    (gdb) "
    If I then stepover it then crashes.

    I tried going to the programs top level dir and running qmake just in case its a QDevelop issue but doing this then double clicking on the exe it crashes out.

  18. #18
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: begginers question

    Well there you have it, each call to addSpreadSheet() creates a new subwindow with

    Qt Code:
    1. QMdiSubWindow *subWindow = mdiArea->addSubWindow(spreadsheet);
    To copy to clipboard, switch view to plain text mode 
    reusing the subWindow pointer and orphaning previous subWindows, thereby confusing the poor old viewport which ends up holding multiple SubWindows all with the same name.

    You could try re-implementing this making subWindow an array of pointers.

    I assume that the two working debuggers are only using the most recent subWindow.

    Atleast that's what I think is happening!
    Last edited by JD2000; 25th February 2010 at 14:20.

  19. #19
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your post JD2000. I sort of understand but don't know how to implement it.... Are you sure you are a beginner?

  20. #20
    Join Date
    Feb 2010
    Posts
    53
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: begginers question

    Thanks for your post JD2000. I sort of understand but don't know how to implement it.... Are you sure you are a beginner?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.