PDA

View Full Version : Problem with Graphics view



denumbaone
24th February 2011, 22:24
I'm having a little trouble with QGraphicsView, i have a multiple file project and I have the main layout for the gui on dialog.cpp with all of the code for the GraphicsView in the MyGraphicsView.cpp file. I've gotten the GraphicsView to load onto the gui with no problem and it is completely funtional (can pan over and zoom in and out). The problem that I am having is that I have added two buttons onto the layout to also toggle the zooming in and out, and whenever I click on either one the program "unexpectedly finished." I have attached the important code below:

in MyGraphicsView.h


public slots:
virtual void zoomIn();
virtual void zoomOut();

private:
MyGraphicsView *graph;
MyGraphicsView *graphicsView;

in dialog.h


private:
MyGraphicsView *graphicsView;
MyGraphicsView *graph;
QPushButton *zoomInButton;
QPushButton *zoomOutButton;

in dialog.cpp


zoomInButton = new QPushButton(tr("+"));
zoomInButton->setFixedSize(50,30);
zoomOutButton = new QPushButton(tr("-"));
zoomOutButton->setFixedSize(50,30);
buttonLayout->addStretch();
buttonLayout->addWidget(zoomInButton);
buttonLayout->addWidget(zoomOutButton);

graph = new MyGraphicsView;

connect(zoomInButton, SIGNAL(clicked()), graph, SLOT(zoomIn()));
connect(zoomOutButton, SIGNAL(clicked()), graph, SLOT(zoomOut()));

and in MyGraphicsView.cpp


void MyGraphicsView::zoomIn(/*QKeyEvent * event*/)
{
cout << "Zoom in\n" << endl;
graphicsView->scale(2.0, 2.0);
}

void MyGraphicsView::zoomOut(/*QKeyEvent * event*/)
{
cout << "Zoom Out\n" << endl;
graphicsView->scale(0.5, 0.5);
}

As you can see, I have added a simple "cout" to make sure that the funtion is called, and it does. It's only when I click on the buttons that the program "unexpectedly finishes."

I apologize for the lengthy post, I only wanted to be as detailed as possible.

Thank you in advance for the help

wysota
24th February 2011, 22:36
Do you initialize both graphicsView and both graph (total of four) variables?

denumbaone
24th February 2011, 23:37
I have posted everything I have in my program that is related to the problem.

wysota
25th February 2011, 11:42
So fix your program by initializing all four views properly. Your program crashes due to dereferencing dangling pointers.