summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Goffart <olivier.goffart@nokia.com>2010-08-13 10:23:46 (GMT)
committerToby Tomkins <toby.tomkins@nokia.com>2010-08-18 12:21:14 (GMT)
commit81bb682e280dc282ba965051e232f81f64c1f3fe (patch)
tree801d912bd01440a8d2a614c0cfe9220976d583d0
parent279c5eb3bd7c8bb836f2a3f8f5a0b1dc046dc24a (diff)
downloadQt-81bb682e280dc282ba965051e232f81f64c1f3fe.zip
Qt-81bb682e280dc282ba965051e232f81f64c1f3fe.tar.gz
Qt-81bb682e280dc282ba965051e232f81f64c1f3fe.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 (cherry picked from commit 5dedcd3c4cac993949295d109804a8f7cc3a5b0a)
-rw-r--r--doc/src/examples/diagramscene.qdoc3
-rw-r--r--examples/graphicsview/diagramscene/mainwindow.cpp29
2 files changed, 22 insertions, 10 deletions
diff --git a/doc/src/examples/diagramscene.qdoc b/doc/src/examples/diagramscene.qdoc
index f4d6b0d..7c643c2 100644
--- a/doc/src/examples/diagramscene.qdoc
+++ b/doc/src/examples/diagramscene.qdoc
@@ -265,7 +265,8 @@
\snippet examples/graphicsview/diagramscene/mainwindow.cpp 3
- This slot deletes the selected item, if any, from the scene. If
+ This slot deletes the selected item, if any, from the scene. It
+ deletes the arrows first in order to avoid to delete them twice. If
the item to be deleted is a \c DiagramItem, we also need to delete
arrows connected to it; we don't want arrows in the scene that
aren't connected to items in both ends.
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));