almo
9th May 2012, 18:54
Hi!
I'm coming from experience with Cocoa and Winforms, and I'm just starting with Qt. I have set up a subclass of QGraphicsView. I'll show the relevant functions below. The issue I'm having is twofold:
If I click item 1 and drag, item 2 moves. If I click item 2 and drag, item 1 moves. :confused:
If I use the mousewheel to zoom, the mouseMoveEvent starts being called whenever the mouse moves even if a button is not held.
I've been staring at this for over an hour, having tried different things, like forcing mouseTracking to false at the end of the wheelevent, which didn't change anything.
EditorView::EditorView(QWidget* parent) :
QGraphicsView(parent)
{
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QGraphicsScene* scene = new QGraphicsScene(this);
setScene(scene);
setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
QGraphicsEllipseItem* item1 = new QGraphicsEllipseItem(0, scene);
item1->setRect(250.0, 250.0, 50.0, 50.0);
item1->setFlag(QGraphicsItem::ItemIsMovable);
QGraphicsEllipseItem* item2 = new QGraphicsEllipseItem(0, scene);
item2->setRect(150.0, 150.0, 50.0, 50.0);
item2->setFlag(QGraphicsItem::ItemIsMovable);
setCursor(Qt::OpenHandCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
}
void EditorView::mousePressEvent(QMouseEvent* event)
{
QGraphicsView::mousePressEvent(event);
m_lastDragPos = event->pos();
}
void EditorView::mouseMoveEvent(QMouseEvent* event)
{
QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
PanView(delta);
m_lastDragPos = event->pos();
}
void EditorView::wheelEvent(QWheelEvent* event)
{
int delta = event->delta() / 120;
ScaleView(delta);
}
void EditorView::PanView(QPointF delta)
{
QPoint viewCenter(viewport()->width() / 2 - delta.x(), viewport()->height() / 2 - delta.y());
QPointF newCenter = mapToScene(viewCenter);
centerOn(newCenter);
}
void EditorView::ScaleView(int delta)
{
bool zoomIn = (delta > 0);
delta = abs(delta);
ViewportAnchor oldAnchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
for(int i = 0; i < delta; ++i)
{
if(zoomIn)
{
scale(1.10, 1.10);
}
else
{
scale(0.90, 0.90);
}
}
setTransformationAnchor(oldAnchor);
}
I'm coming from experience with Cocoa and Winforms, and I'm just starting with Qt. I have set up a subclass of QGraphicsView. I'll show the relevant functions below. The issue I'm having is twofold:
If I click item 1 and drag, item 2 moves. If I click item 2 and drag, item 1 moves. :confused:
If I use the mousewheel to zoom, the mouseMoveEvent starts being called whenever the mouse moves even if a button is not held.
I've been staring at this for over an hour, having tried different things, like forcing mouseTracking to false at the end of the wheelevent, which didn't change anything.
EditorView::EditorView(QWidget* parent) :
QGraphicsView(parent)
{
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
QGraphicsScene* scene = new QGraphicsScene(this);
setScene(scene);
setSceneRect(INT_MIN / 2, INT_MIN / 2, INT_MAX, INT_MAX);
QGraphicsEllipseItem* item1 = new QGraphicsEllipseItem(0, scene);
item1->setRect(250.0, 250.0, 50.0, 50.0);
item1->setFlag(QGraphicsItem::ItemIsMovable);
QGraphicsEllipseItem* item2 = new QGraphicsEllipseItem(0, scene);
item2->setRect(150.0, 150.0, 50.0, 50.0);
item2->setFlag(QGraphicsItem::ItemIsMovable);
setCursor(Qt::OpenHandCursor);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOf f);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
}
void EditorView::mousePressEvent(QMouseEvent* event)
{
QGraphicsView::mousePressEvent(event);
m_lastDragPos = event->pos();
}
void EditorView::mouseMoveEvent(QMouseEvent* event)
{
QPointF delta = mapToScene(event->pos()) - mapToScene(m_lastDragPos);
PanView(delta);
m_lastDragPos = event->pos();
}
void EditorView::wheelEvent(QWheelEvent* event)
{
int delta = event->delta() / 120;
ScaleView(delta);
}
void EditorView::PanView(QPointF delta)
{
QPoint viewCenter(viewport()->width() / 2 - delta.x(), viewport()->height() / 2 - delta.y());
QPointF newCenter = mapToScene(viewCenter);
centerOn(newCenter);
}
void EditorView::ScaleView(int delta)
{
bool zoomIn = (delta > 0);
delta = abs(delta);
ViewportAnchor oldAnchor = transformationAnchor();
setTransformationAnchor(QGraphicsView::AnchorUnder Mouse);
for(int i = 0; i < delta; ++i)
{
if(zoomIn)
{
scale(1.10, 1.10);
}
else
{
scale(0.90, 0.90);
}
}
setTransformationAnchor(oldAnchor);
}