Reputation: 1217
I have subclassed my own qgraphicsview because i wanted to send signals from scene with positions when clicked, on this scene are also my sublclassed graphics itms which are selectable and focusable. The problem is, when i implement my own mousepressed/mousemoved/mousereleased event handlers into the scene, I suddenly cannot select anything in the scene.
Here's my implementation of the graphics scene:
/////////////////////HEADER//////////////
#include <QGraphicsScene>
class myGraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
explicit myGraphicsScene(QObject * parent = 0);
explicit myGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject * parent = 0);
signals:
void mouseReleased(QPointF pos);
void mousePressed(QPointF pos);
void mouseMoved(QPointF pos);
void mouseDoubleClicked(QPointF pos);
protected:
void mouseDoubleClickEvent (QGraphicsSceneMouseEvent * event);
void mousePressEvent(QGraphicsSceneMouseEvent * event);
void mouseMoveEvent(QGraphicsSceneMouseEvent * event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent * event);
////////////////////CPP//////////////////////
#include <QGraphicsSceneMouseEvent>
myGraphicsScene::myGraphicsScene(QObject *parent) : QGraphicsScene(parent)
{
}
myGraphicsScene::myGraphicsScene(qreal x, qreal y, qreal width, qreal height, QObject *parent)
: QGraphicsScene( x, y, width, height, parent )
{
}
void myGraphicsScene::mouseDoubleClickEvent ( QGraphicsSceneMouseEvent *event )
{
QGraphicsScene::mouseDoubleClickEvent ( event );
emit mouseDoubleClicked ( event->scenePos () );
}
void myGraphicsScene::mousePressEvent (QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mousePressEvent ( event );
emit mousePressed ( event->scenePos () );
}
void myGraphicsScene::mouseMoveEvent (QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mouseMoveEvent ( event );
emit mouseMoved ( event->scenePos () );
}
void myGraphicsScene::mouseReleaseEvent (QGraphicsSceneMouseEvent *event)
{
QGraphicsScene::mousePressEvent ( event );
emit mouseReleased ( event->scenePos () );
}
Which event implementation is wrong?
P.S.: When i comment out the mousePressed/moved/released implementations it works like charm.
Upvotes: 2
Views: 892
Reputation: 27639
I suspect the problem is that calling events such as QGraphicsScene::mouseMoveEvent, when called like this, may be setting the event to being handled. As QGraphicsSceneMouseEvent states for the accepted flag: -
Setting the accept parameter indicates that the event receiver wants the event. Unwanted events might be propagated to the parent widget. By default, isAccepted() is set to true, but don't rely on this as subclasses may choose to clear it in their constructor.
You could try calling event->setAccepted(false) in each of the overloaded events to signal the event to be propagated.
Upvotes: 2