PDA

View Full Version : Crashes When Using QGraphicsScene?



steadi
13th October 2012, 18:22
Hello,

I am still pretty new to Qt and it is pretty different from most of the editors I have ever used before. I have started a simple project - When you click on a QGraphicsScene it draws a red background. However, upon clicking on the scene it crashes the program.

Can anyone see where I went wrong, for the life of me I cannot figure this one out?

Scene Class:


class scene2 : public QGraphicsScene
{
#include <QMessageBox>

public:
QGraphicsScene *scene;
void mousePressEvent(QGraphicsSceneMouseEvent *event);
};

Set's Scene To View:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

scene = new scene2;
ui->GraphicsMap->setScene(scene);
scene->setSceneRect(0,0,900,900);
}

On Click:

void scene2::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QBrush whiteBrush(Qt::red);
scene->setBackgroundBrush(whiteBrush);
}

Thanks for any help guys,
Matt <Noob>

wysota
13th October 2012, 18:47
Please post the whole project, if you can. What you posted seems ok, it shouldn't cause a crash.

steadi
13th October 2012, 18:58
Thanks for the help man,

It appears to be working now. It turns out that I was actually decalring two scene variables named scene and each one with there own class that was cancelling out the others commands and crashing the system.

Now that I have that working - I can click on the scene and get a red background but I cannot drag items that have the ItemIsMovable flag on, why is this?

Thanks,
Matt

wysota
13th October 2012, 20:06
Because the flag is implemented in scene's mousePressEvent. You have overriden it without calling the base class implementation, so the code that handles moving items is not called. If you call the base class implementation of the mousePressEvent, all should work fine.