I've subclassed QGraphicsView for a custom canvas to be used in a CAD application. I've successfully reimplemented QGraphicsView::wheelEvent to check the keyboard modifiers for the control key and, if the control key is pressed, to zoom. I'm trying to implement a horizontal scroll when the user holds shift and uses the wheel.
The problem I'm having is that the horizontal scrolling also always scrolls up by 0.279. Not a huge problem, but hugely annoying and it points to something else being wrong.
So, here are the questions:
- Is this the right way to implement horizontal scrolling? If not, what is?
- How do I eliminate this delta of 0.279?
Thanks in advance. Code and sample output below
void myView::zoom(int delta)
{
double factor = pow(1.2, delta/abs(delta));
this->scale(factor, factor);
}
void myView::scrollHorizontal(int level)
{
QPointF center = mapToScene(viewport()->rect().center());
qDebug() << "center: " << center.x() << ", " << center.y();
centerOn(QPointF(center.x() - level, center.y()));
}
void myView::wheelEvent(QWheelEvent *event)
{
//qDebug() << "delta: " << event->delta();
if (event->modifiers() & Qt::ControlModifier)
{
this->zoom(event->delta());
}
else if (event->modifiers() & Qt::ShiftModifier)
{
this->scrollHorizontal(event->delta());
}
else
QGraphicsView::wheelEvent(event);
}
sample output from the qDebug() line in scrollHorizontal when at the left-edge of the scene:
center: 261.5 , 615.654
center: 261.5 , 615.375
center: 261.5 , 615.096
center: 261.5 , 614.817
center: 261.5 , 614.538
center: 261.5 , 614.259
center: 261.5 , 613.98
center: 261.5 , 613.701
center: 261.5 , 613.421
QGraphicsView
scrolling a little 'unnatural' when you reach the bounds of the scene rectangle, is the behaviour the same regardless of where the visible rect is in the scene rect? – cmannett85