Problems with QGraphicsItem and Overiding methods
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.
Re: Problems with QGraphicsItem and Overiding methods
Could you show the DGraphicsRectItem::mousePressEvent() function declaration? Notice that it's mousePressEvent() not MousePressEvent().
Re: Problems with QGraphicsItem and Overiding methods
Code:
#pragma once
#include <QtGui/QGraphicsRectItem>
#include "DGraphicsItem.h"
class DGraphicsRectItem :
{
public:
DGraphicsRectItem(void);
~DGraphicsRectItem(void);
void instantiateDescriptors();
};
Code:
#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)
{
}
{
// QGraphicsRectItem::mousePressEvent(mouseEvent);
std::cout << "DGraphicsRectItem::mousePressEvent";
}
std::cout << "DGraphicsRectItem::mouseMoveEvent";
}
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
}
Re: Problems with QGraphicsItem and Overiding methods
How do you know DGraphicsRectItem::mousePressEvent() doesn't get called? Perhaps it's just the missing std::endl...
Re: Problems with QGraphicsItem and Overiding methods
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
Re: Problems with QGraphicsItem and Overiding methods
If you override QGraphicsScene mouse event handlers, you must call the base class implementation if you want any item to receive mouse events.
Re: Problems with QGraphicsItem and Overiding methods
Quote:
Originally Posted by
jpn
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?
Re: Problems with QGraphicsItem and Overiding methods
Code:
void SubClass::virtualFunction()
{
BaseClass::virtualFunction(); // <---
// ...
}
Re: Problems with QGraphicsItem and Overiding methods
where subclass is my subclass of QGraphicsScene
in this case called BoardModel
and BaseClass is QGraphicsScene itself?
and virtualfunction is mouseMoveEvent?
Re: Problems with QGraphicsItem and Overiding methods
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.
Re: Problems with QGraphicsItem and Overiding methods
Quote:
Originally Posted by
jpn
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!