Problems with event handlers of my custom subclass of QGraphicsScene.
-
Greetings.
I am implementing the mouse event handlers for my custom subclass of QGraphicsScene, but does not work.
I wonder if I will also need to implement the respective event handlers for my subclass of QGraphicsView too? ... Can this be the cause of the problem or not necessarily?.
Currently my QGraphicsView subclass does not implement any custom event handler.
Thanks in advance for any help and/or suggestion
-
What does not work exactly ?
And ... no you don't have to implement event handlers for QGraphicsView.
-
What your problem is, is not clear at all from your posting.
However, a common mistake when reimplementing (event) handlers, is to not call the baseclass implementation from your own implementation. Unless you're really sure that you want to override rather than augment the default event handling system, you should call the baseclass implementation of the function you're reimplementing.
Make it your default way of working: when reimplementing a virtual function from a baseclass, you start out like this:
@
bool MySubclass:theReimplementedFunction(Foo arg1, Bar arg2)
{
bool result = TheBaseClass:theReimplementedFunction(arg1, arg2);return result;
}
@Only then do you start to add your own code above or between it. And only if you are certain, you remove the line calling the baseclass implementation or make it conditional.
-
Greetings.
At first I did not explain what the problem was because I thought it was due to not have implemented the mouse event handlers for my subclass of QGraphicsView.
The issue is as follows:
First I create a subclass of QGraphicsItem called GraficRoi (Roi by Region Of Interest). This class essentially draws (and manages events for) a rectangle with 8 handgrips as shown in the next screenshot.
!https://dl.dropboxusercontent.com/u/31139483/toma_de_pantalla.png(screenshot)!
Also create a subclass of QGraphicsScene called EscenaImagen for which I implemented the mouse event handlers as follows:
@
void EscenaImagen::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if( eventPersActiv == MarcrRoiEnabled )
p1 = p2 = event->pos();QGraphicsScene::mousePressEvent( event );
}
void EscenaImagen::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
if( eventPersActiv == MarcrRoiEnabled )
{
if( roiEnConstrc )
removeItem( roiEnConstrc );p2 = event->pos(); QPointF esqnSupIzq( qMin(p1.x(),p2.x()), qMin(p1.y(),p2.y()) ); QSize dim( qAbs(p1.x() - p2.x()), qAbs(p1.y() - p2.y()) ); roiEnConstrc = addRect(QRectF(esqnSupIzq, dim), PenRoiEnConstrc); update(); } QGraphicsScene::mouseMoveEvent( event );
}
void EscenaImagen::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if( eventPersActiv == MarcrRoiEnabled )
{
rois.append( new GraficRoi(roiEnConstrc->rect(), 0) );
addItem( rois.last() );removeItem( roiEnConstrc ); roiEnConstrc = 0; update(); } QGraphicsScene::mouseReleaseEvent( event );
}
@The idea is to allow, when the flag 'eventPersActiv' has the value 'MarcrRoiEnabled' the user to click on the left mouse button on a point in the scene and then move the mouse (keeping the button pressed) go drawing interactively a rectangle (a QGraphicsRectItem* named 'RoiEnConstruc'). When the user releases the left mouse button, the rectangle drawn is replaced by a GraficRoi (which the user can resize using its 8 handgrips).
Also create a subclass of QGraphicsView called 'Vista' (view in Spanish). This subclass only has a couple of slots that handle zoom in and zoom out according to a predefined scale.
The problem is that the event handlers 'EscenaImage' (apparently) do nothing, ie, I click on a point in the scene and move the mouse, but nothing is drawn.
Because I do not implement the respective mouse event handlers for the class Vista, I thought (in principle) that the problem was due to this missing part. For that reason ask whether "it was necessary" to implement event handlers subclass QGraphicsView too.
Any ideas about the origin of this problem or solution?