PDA

View Full Version : QGraphicsScene::drawItems is not called if it's subclassed



jano_alex_es
16th June 2010, 12:27
Hi,

I have my subclassed graphicsview and graphicsScene, all works perfectly with more or less 100 graphicsitems and some logic implemented in my custom scene.

I want to control when the items appear, so I tried to subclass QGraphicsScene::drawItems. But when QGraphicsView::drawItems is called it does not go to my subclassed scene, it calls QGraphicsScene::drawItems.

I can solve it writting my code directly in the view... but I would like to ask if it's the normal behavour or I'm missing something.

Thanks!

tbscope
16th June 2010, 17:23
But when QGraphicsView::drawItems is called it does not go to my subclassed scene, it calls QGraphicsScene::drawItems.

QGraphicsView calls drawItems of the scene that is added to the view, thus your custom scene.
This in turn means that you did something wrong in your scene subclass.

Post your code.

jano_alex_es
16th June 2010, 21:22
QGraphicsView calls drawItems of the scene that is added to the view, thus your custom scene.
that's the problem... but it doesn't :S




MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setWindowTitle("PruebaMagnetica1");

m_pScene = new CScene();
m_pView = new CView(m_pScene, this);
...
}



View.h


class CView : public QGraphicsView
{
public:
CView(CScene* pScene, QWidget * parent = 0);
virtual ~CView();

void enableRendering(){m_bRender = true;}

protected:
void drawItems ( QPainter * painter, int numItems, QGraphicsItem * items[],
const QStyleOptionGraphicsItem options[] );

bool m_bRender;
};





View.cpp


CView::CView(CScene* pScene, QWidget * parent)
{
setParent(parent);
setScene(pScene);

m_bRender = false;
}

CView::~CView()
{
// TODO Auto-generated destructor stub
}

void CView::drawItems( QPainter * painter, int numItems, QGraphicsItem * items[], const QStyleOptionGraphicsItem options[] )
{
if(m_bRender)
{
QGraphicsView::drawItems(painter, numItems, items, options); //It calls QGraphicsScene::drawItems instead of CScene::drawItems
m_bRender = false;
}
}

wysota
16th June 2010, 21:57
How do you know it calls QGraphicsScene::drawItems()?

jano_alex_es
17th June 2010, 07:03
because I have it subclassed as well in "CScene". But debugging it goes into QGraphicsScene class and not into the subclassed one.

Scene.h

class CScene : public QGraphicsScene
{
Q_OBJECT

public:
CScene(QObject *parent = 0);
virtual ~CScene();

protected:
void drawItems ( QPainter * painter, int numItems, QGraphicsItem * items[],
const QStyleOptionGraphicsItem options[] );




Scene.cpp


void CScene::drawItems( QPainter * painter, int numItems, QGraphicsItem * items[], const QStyleOptionGraphicsItem options[] )
{
//
//my code here, with a not called breakpoint
//

QGraphicsScene::drawItems(painter, numItems, items, options);
}

tbscope
17th June 2010, 07:34
Can you post the line of code where you set the breakpoint?
Some debuggers don't break on certain lines of code.

jano_alex_es
17th June 2010, 07:48
I tried using traces as well :(

For instance, if I try this in order to test:


void CScene::drawItems( QPainter * painter, int numItems, QGraphicsItem * items[], const QStyleOptionGraphicsItem options[] )
{
QGraphicsScene::drawItems(painter, numItems, items, options);//breakpoint
}

doesn't work either

jano_alex_es
17th June 2010, 07:52
just in case, some extra info: I'm using Visual Studio 2008 with Qt 4.5.0 (unfortunatly I can't update)

wysota
17th June 2010, 08:38
Your method has an incorrect signature, you are missing the last argument (QWidget pointer).

jano_alex_es
17th June 2010, 09:53
buf, I'm so stupid... thanks :)