Results 1 to 20 of 24

Thread: begginers question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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);

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

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

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

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

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

    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.

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

  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

    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 15:20.

  9. #9
    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?

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

  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

    Well there you have it, each call to addSpreadSheet() creates a new subwindow with
    Very good JD200, I totally missed that one.

    I sort of understand but don't know how to implement it....
    You can use a QVector to manage your subWindows.
    ==========================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 but I haven't a clue how to implement this.

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

    Default Re: begginers question

    Solved.
    Qt Code:
    1. MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f)
    2. : QMainWindow(parent, f)
    3. { // on constructor
    4. setupUi(this); // required so menues appear
    5.  
    6. mdiArea = new QMdiArea;
    7. mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    8. mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    9. setCentralWidget(mdiArea);
    10. setWindowTitle(tr("Spreadsheet"))
    11.  
    12.  
    13. }
    14. void MainWindowImpl::createMdiWindow(const QString &fileName)
    15. {
    16. Spreadsheet *spreadsheet = new Spreadsheet;
    17. QMdiSubWindow *subWindow = mdiArea->addSubWindow(spreadsheet);
    18. subWindow->show();
    19. if (!fileName.isEmpty()) {
    20. // spreadsheet->load(fileName);
    21. statusBar()->showMessage(tr("File loaded"), 2000);
    22. }
    23. }
    24.  
    25. void MainWindowImpl::newFile()
    26. {
    27. createMdiWindow();
    28. }
    29.  
    30. Spreadsheet *MainWindowImpl::activeSpreadsheet()
    31. {
    32. QMdiSubWindow *subWindow = mdiArea->activeSubWindow();
    33. if (subWindow)
    34. return qobject_cast<Spreadsheet*>(subWindow->widget());
    35. return 0;
    36. }
    To copy to clipboard, switch view to plain text mode 

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