PDA

View Full Version : Problems with QGraphicsItem and Overiding methods



Terabyte
27th December 2008, 16:13
Hi,

My setup, I have a
QGraphicsView Object
QGraphicsScene Object
and a controller object...

The QGraphicsScene returns QGraphicItems usually.

I have a hierachi, where I have created DGraphicRectItems, which derive from QGraphicRectItems.

DGraphicRectItems have mouse methods such as MousePressEvent(); which should overide the QGraphicsRectItems default handling.

However when I press or hover or anything over the DGraphicsRectItem objects on the canvas, nothing happens.

I suspect this is because the mouseEvents are being sent to the QGraphicRectItems since this is what the scene deals with......

But this should not be the case, i should be able to have those D mouse methods run.

Could sombody explain why this is the case?

I have even tried writing mouseEvent->accept(); inside the D mouse method. still nothing happens.

jpn
27th December 2008, 16:24
Could you show the DGraphicsRectItem::mousePressEvent() function declaration? Notice that it's mousePressEvent() not MousePressEvent().

Terabyte
27th December 2008, 16:30
#pragma once
#include <QtGui/QGraphicsRectItem>
#include "DGraphicsItem.h"
class QGraphicsSceneMouseEvent;
class DGraphicsRectItem :
public QGraphicsRectItem, public DGraphicsItem
{
public:
DGraphicsRectItem(void);
~DGraphicsRectItem(void);
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);
void changeSize(QPointF *change);
void instantiateDescriptors();
};




#include "DGraphicsRectItem.h"
#include <iostream>
#include <QPointF>
#include <QtGui/QGraphicsSceneMouseEvent>
DGraphicsRectItem::DGraphicsRectItem(void)
{
this->setFlag(DGraphicsRectItem::ItemIsSelectable,true);
this->setFlag(DGraphicsRectItem::ItemIsMovable,true);
this->setFlag(DGraphicsRectItem::ItemIsFocusable,true);

}

DGraphicsRectItem::~DGraphicsRectItem(void)
{
}

void DGraphicsRectItem::mousePressEvent(QGraphicsSceneM ouseEvent *mouseEvent)
{
// QGraphicsRectItem::mousePressEvent(mouseEvent);
std::cout << "DGraphicsRectItem::mousePressEvent";
}
void DGraphicsRectItem::mouseMoveEvent(QGraphicsSceneMo useEvent *mouseEvent){
std::cout << "DGraphicsRectItem::mouseMoveEvent";
}

void DGraphicsRectItem::mouseReleaseEvent(QGraphicsScen eMouseEvent *mouseEvent){
std::cout << "releaseevent";
}

void DGraphicsRectItem::changeSize(QPointF *change)
{

std::cout << "YES" << std::endl;
}

void DGraphicsRectItem::instantiateDescriptors()
{
std::cout << "DGraphicsRectItem::instantiateDescriptors()" << std::endl;
//DescRect *item = new DescRect();

//instantiate rect descriptor.
//create modify event object

}

jpn
27th December 2008, 16:57
How do you know DGraphicsRectItem::mousePressEvent() doesn't get called? Perhaps it's just the missing std::endl...

Terabyte
27th December 2008, 17:12
I have set a breakpoint.

I should also say that I have overidden GraphicsScene::mouseMoveEvent events.
And these events ARE triggered. since the breakpoint for these events are hit.

I have forwarded the event to another function, and this other function 'ignores the event'.

So the scene mousemove events are being triggered, just not the DGraphicRectItem mouseMove events

jpn
27th December 2008, 17:24
If you override QGraphicsScene mouse event handlers, you must call the base class implementation if you want any item to receive mouse events.

Terabyte
27th December 2008, 17:29
If you override QGraphicsScene mouse event handlers, you must call the base class implementation if you want any item to receive mouse events.

how do i do that?
and where do i do that?

jpn
27th December 2008, 17:30
void SubClass::virtualFunction()
{
BaseClass::virtualFunction(); // <---
// ...
}

Terabyte
27th December 2008, 17:37
where subclass is my subclass of QGraphicsScene
in this case called BoardModel

and BaseClass is QGraphicsScene itself?


and virtualfunction is mouseMoveEvent?

jpn
27th December 2008, 17:43
Yes, exactly. Your reimplementation is called instead of the QGraphicsScene implementation, but the QGraphicsScene implementation is the one who delivers mouse events to the items.

Terabyte
27th December 2008, 17:45
Yes, exactly. Your reimplementation is called instead of the QGraphicsScene implementation, but the QGraphicsScene implementation is the one who delivers mouse events to the items.

thank you, it worked. Spent months wondering about that!