Hi!
I'm having problems with my QGraphicsView and some mouse events. Basically I want to zoom and pan my view and also handle the items events.
I want the following behavior: The graphics view should only receive the events while my space button is pressed. So if you want to pan/zoom you should press space and either the right or left mouse button (left = pan, right = zoom). Otherwise the items should receive the events.
My items don't implement any event handling at the moment so they are using the standard implementation.
The code for my view:
//--------------------------------------------------------------------------------
{
if(_key != Qt::Key_Space)
{
event->ignore();
return;
}
//save mouse position
_last_position = event->pos();
}
//--------------------------------------------------------------------------------
void GraphicsView
::mouseReleaseEvent(QMouseEvent *event
) {
if(_key != Qt::Key_Space)
{
event->ignore();
return;
}
setCursor(_default_cursor);
}
//--------------------------------------------------------------------------------
{
if(_key != Qt::Key_Space)
{
event->ignore();
return;
}
int dx = event->x() - _last_position.x();
int dy = event->y() - _last_position.y();
//save mouse position
_last_position = event->pos();
qDebug() << "pos: " << event->pos().x() << " " << event->pos().y();
if (event->buttons() == Qt::LeftButton)
{
setCursor(_move_cursor);
pan(dx, dy);
}
else if (event->buttons() == Qt::RightButton)
{
zoom(dx);
}
}
//--------------------------------------------------------------------------------
void GraphicsView
::keyPressEvent(QKeyEvent *event
) {
_key = event->key();
}
//--------------------------------------------------------------------------------
void GraphicsView
::keyReleaseEvent(QKeyEvent *event
) {
_key = 0;
}
//--------------------------------------------------------------------------------
void GraphicsView::mousePressEvent(QMouseEvent *event)
{
if(_key != Qt::Key_Space)
{
QGraphicsView::mousePressEvent(event);
event->ignore();
return;
}
//save mouse position
_last_position = event->pos();
}
//--------------------------------------------------------------------------------
void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if(_key != Qt::Key_Space)
{
QGraphicsView::mouseReleaseEvent(event);
event->ignore();
return;
}
setCursor(_default_cursor);
}
//--------------------------------------------------------------------------------
void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if(_key != Qt::Key_Space)
{
QGraphicsView::mouseMoveEvent(event);
event->ignore();
return;
}
int dx = event->x() - _last_position.x();
int dy = event->y() - _last_position.y();
//save mouse position
_last_position = event->pos();
qDebug() << "pos: " << event->pos().x() << " " << event->pos().y();
if (event->buttons() == Qt::LeftButton)
{
setCursor(_move_cursor);
pan(dx, dy);
}
else if (event->buttons() == Qt::RightButton)
{
zoom(dx);
}
}
//--------------------------------------------------------------------------------
void GraphicsView::keyPressEvent(QKeyEvent *event)
{
_key = event->key();
}
//--------------------------------------------------------------------------------
void GraphicsView::keyReleaseEvent(QKeyEvent *event)
{
_key = 0;
}
To copy to clipboard, switch view to plain text mode
Now my main problem is that I receive all events as expected but during my views mousMoveEvent which I handle only while space is pressed my events position isn't correct. It stays always the same. If I don't have space pressed the events position changes depending on the mouse position.
I have no idea what I'm doing wrong. If I uncomment all the QGraphicsView::mouseXEvent(event) it works fine. But if I deliver the events to my items if space is not pressed it doesn't work.
Bookmarks