PDA

View Full Version : Subclassing QGraphicsView crash the app



guidupas
22nd May 2014, 14:43
Hello all!

I am trying to subclass QGraphicsView, but its crashing the app.

How can I do that.

fluxocaixagraphicsview.h


#ifndef FLUXOCAIXAGRAPHICSVIEW_H
#define FLUXOCAIXAGRAPHICSVIEW_H

#include <QGraphicsView>
#include <QGraphicsScene>
//#include <QGraphicsLineItem>

class fluxoCaixaGraphicsView : public QGraphicsView
{
public:
explicit fluxoCaixaGraphicsView(QWidget *parent = 0);
~fluxoCaixaGraphicsView();

private:
QGraphicsScene *cenario;

protected:
void showEvent(QShowEvent *event);
void resizeEvent(QResizeEvent *event);
};

#endif // FLUXOCAIXAGRAPHICSVIEW_H


fluxocaixagraphicsview.cpp


#include "fluxocaixagraphicsview.h"

fluxoCaixaGraphicsView::fluxoCaixaGraphicsView(QWi dget *parent) :
QGraphicsView(parent)
{
QGraphicsScene *cenario = new QGraphicsScene(this);
}

fluxoCaixaGraphicsView::~fluxoCaixaGraphicsView()
{

}

void fluxoCaixaGraphicsView::showEvent(QShowEvent *event)
{

}

void fluxoCaixaGraphicsView::resizeEvent(QResizeEvent *event)
{

}


calling it


QWidget *MainWindow::criaEstruturaNovoIvestimento()
{
//code here

fluxoCaixaGraphicsView *graphicsViewFluxoCaixa = new fluxoCaixaGraphicsView(boxInvestimento3);

//code here
}


Thanks a lot

stampede
22nd May 2014, 19:26
fluxoCaixaGraphicsView::fluxoCaixaGraphicsView(QWi dget *parent) :
QGraphicsView(parent)
{
QGraphicsScene *cenario = new QGraphicsScene(this);
}

you just declared a local variable named "cenario", it is not the same as your "cenario" in the header file
change this to


fluxoCaixaGraphicsView::fluxoCaixaGraphicsView(QWi dget *parent) :
QGraphicsView(parent)
{
this->cenario = new QGraphicsScene(this);
}

btw. you should call base class implementation of the reimplemented event handlers.

d_stranz
22nd May 2014, 23:24
Also need to insert the Q_OBJECT macro in the fluxoCaixaGraphicsView class definition:



class fluxoCaixaGraphicsView : public QGraphicsView
{
Q_OBJECT

public:
explicit fluxoCaixaGraphicsView(QWidget *parent = 0);
~fluxoCaixaGraphicsView();

// ...
};