diff options
author | Olivier Goffart <olivier.goffart@nokia.com> | 2010-08-16 15:35:17 (GMT) |
---|---|---|
committer | Liang Qi <liang.qi@nokia.com> | 2010-08-16 15:35:17 (GMT) |
commit | 34eb6cb58629c58b819d4c82b582106513da03bd (patch) | |
tree | 6281c6a197f8c38d1fd2471363243df79d64b47e /examples | |
parent | bc2a71b317724840c695b39f523ca510c28d15e5 (diff) | |
download | Qt-34eb6cb58629c58b819d4c82b582106513da03bd.zip Qt-34eb6cb58629c58b819d4c82b582106513da03bd.tar.gz Qt-34eb6cb58629c58b819d4c82b582106513da03bd.tar.bz2 |
diagramscene example: fix leak and crashes.
In MainWindow::deleteItem, if there is arrow selected, we need to clean
up the items from this arrow. We need to delete them first so there is no
arrow in the list anymore when we calls removeArrows (which before,
deleted arrow that were possibly in the list, resulting in crashes)
Also avoid leak by giving parents to objects that needs it, and destroying items.
Reviewed-by: Geir Vattekar
Task-number: QTBUG-12753
Merge-request: 775
Reviewed-by: Liang Qi <liang.qi@nokia.com>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/graphicsview/diagramscene/mainwindow.cpp | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/examples/graphicsview/diagramscene/mainwindow.cpp b/examples/graphicsview/diagramscene/mainwindow.cpp index f09c552..7d82df0 100644 --- a/examples/graphicsview/diagramscene/mainwindow.cpp +++ b/examples/graphicsview/diagramscene/mainwindow.cpp @@ -45,6 +45,7 @@ #include "diagramitem.h" #include "diagramscene.h" #include "diagramtextitem.h" +#include "arrow.h" const int InsertTextButton = 10; @@ -55,7 +56,7 @@ MainWindow::MainWindow() createToolBox(); createMenus(); - scene = new DiagramScene(itemMenu); + scene = new DiagramScene(itemMenu, this); scene->setSceneRect(QRectF(0, 0, 5000, 5000)); connect(scene, SIGNAL(itemInserted(DiagramItem*)), this, SLOT(itemInserted(DiagramItem*))); @@ -123,11 +124,22 @@ void MainWindow::buttonGroupClicked(int id) void MainWindow::deleteItem() { foreach (QGraphicsItem *item, scene->selectedItems()) { - if (item->type() == DiagramItem::Type) { - qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); + if (item->type() == Arrow::Type) { + scene->removeItem(item); + Arrow *arrow = qgraphicsitem_cast<Arrow *>(item); + arrow->startItem()->removeArrow(arrow); + arrow->endItem()->removeArrow(arrow); + delete item; } - scene->removeItem(item); } + + foreach (QGraphicsItem *item, scene->selectedItems()) { + if (item->type() == DiagramItem::Type) { + qgraphicsitem_cast<DiagramItem *>(item)->removeArrows(); + } + scene->removeItem(item); + delete item; + } } //! [3] @@ -313,7 +325,7 @@ void MainWindow::about() //! [21] void MainWindow::createToolBox() { - buttonGroup = new QButtonGroup; + buttonGroup = new QButtonGroup(this); buttonGroup->setExclusive(false); connect(buttonGroup, SIGNAL(buttonClicked(int)), this, SLOT(buttonGroupClicked(int))); @@ -345,7 +357,7 @@ void MainWindow::createToolBox() QWidget *itemWidget = new QWidget; itemWidget->setLayout(layout); - backgroundButtonGroup = new QButtonGroup; + backgroundButtonGroup = new QButtonGroup(this); connect(backgroundButtonGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(backgroundButtonGroupClicked(QAbstractButton*))); @@ -460,7 +472,6 @@ void MainWindow::createToolbars() editToolBar->addAction(sendBackAction); fontCombo = new QFontComboBox(); - fontSizeCombo = new QComboBox(); connect(fontCombo, SIGNAL(currentFontChanged(QFont)), this, SLOT(currentFontChanged(QFont))); @@ -526,7 +537,7 @@ void MainWindow::createToolbars() linePointerButton->setCheckable(true); linePointerButton->setIcon(QIcon(":/images/linepointer.png")); - pointerTypeGroup = new QButtonGroup; + pointerTypeGroup = new QButtonGroup(this); pointerTypeGroup->addButton(pointerButton, int(DiagramScene::MoveItem)); pointerTypeGroup->addButton(linePointerButton, int(DiagramScene::InsertLine)); @@ -605,7 +616,7 @@ QMenu *MainWindow::createColorMenu(const char *slot, QColor defaultColor) names << tr("black") << tr("white") << tr("red") << tr("blue") << tr("yellow"); - QMenu *colorMenu = new QMenu; + QMenu *colorMenu = new QMenu(this); for (int i = 0; i < colors.count(); ++i) { QAction *action = new QAction(names.at(i), this); action->setData(colors.at(i)); |