Wild guess: Check if scene() returns a valid pointer in GraphicsView's destructor.
Wild guess: Check if scene() returns a valid pointer in GraphicsView's destructor.
I would suspect there is a parent-child relationship between some objects that causes double deletion of something but I can't see anything like that in the code.
Maybe scene tries to delete your rect created on stack. Try to call removeItem() before clear() and see if it still crashes:Qt Code:
// qgraphicsscene.cpp //... // Recursive descent delete for (int i = 0; i < d->indexedItems.size(); ++i) { if (!item->parentItem()) delete item; //!< here } }To copy to clipboard, switch view to plain text mode
Qt Code:
GraphicsView::~GraphicsView() { scene()->removeItem(&rect); scene()->clear(); }To copy to clipboard, switch view to plain text mode
If the lifespan of the scene is tied to the lifespan of the view then the easiest way is to make the scene a dynamically allocated object with its parent set to the view. Then you don't have to clear the scene because the scene's destructor will do it for you. Maybe the problem will go away then. Also, what is causing the view to be deleted?
K.Sejdak (13th March 2011)
That was it! I did just as you said and it worked perfectly. In fact, it is much simplier and logical to modify an existing widget this way, than to replace it. Thanks for all your help. I will provide the changed code for the others with similar problems:
Qt Code:
#include "MainWindow.h" #include "ui_MainWindow.h" { ui->setupUi(this); view = new GraphicsView(ui->widget); // <--- here view->setScene(&scene); // view->resize(ui->widget->size()); // connect(ui->closeButton, SIGNAL(clicked()), this, SLOT(close())); connect(ui->loadButton, SIGNAL(clicked()), this, SLOT(loadImage())); } MainWindow::~MainWindow() { delete view; delete ui; } void MainWindow::loadImage() { image.load(fileName); scene.addPixmap(image); view->loadImage(image); }To copy to clipboard, switch view to plain text mode
So what exactly did you change? Because if you mean line #7 of the above snippet then it's certainly not what I meant and it shouldn't have any influence on your application.
Hmm, now I realized that when I was reading your post I saw "view" instead of "scene" in part about dynamic allocation. Nevertheless it somehow solved the problem with the exit code. Do you think that it was just a luck? Maybe there is another mistake?
And to answer your question: I just dynamically allocated memory for view object and I set wiget from the main form to be a parent for it.
Last edited by K.Sejdak; 13th March 2011 at 20:37.
"My programs don't have bugs. They just provide random features."
If you look inside setupUi(), that's exactly what was happening previously too.
Bookmarks