我已经将我自己的qgraphics视图子类化,因为我想在点击时从场景发送信号,在这个场景上也是我可以选择和聚焦的子图形图形。问题是,当我将自己的mousepressed / mousemoved / mousereleased事件处理程序实现到场景中时,我突然无法在场景中选择任何内容。
这是我对图形场景的实现:
/////////////////////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 () );
}
哪个事件实施有误?
P.S。:当我注释掉mousePressed / moving / release实现时,它就像魅力一样。
答案 0 :(得分:2)
我怀疑问题是调用QGraphicsScene :: mouseMoveEvent这样的事件时,可能会将事件设置为正在处理。由于QGraphicsSceneMouseEvent声明了accepted标志: -
设置accept参数表示事件接收者想要该事件。不需要的事件可能会传播到父窗口小部件。 默认情况下,isAccepted()设置为true ,但不要依赖于此,因为子类可能会选择在构造函数中清除它。
您可以尝试在每个重载事件中调用event-&gt; setAccepted(false)来表示要传播的事件。