Results 1 to 20 of 24

Thread: begginers question

Hybrid View

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

    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.

  2. #2
    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

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

    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.

  4. #4
    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 

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

    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.

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

    Default Re: begginers question

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

  7. #7
    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)

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

    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);

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

    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.

  10. #10
    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

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

    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.

  12. #12
    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 

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.