PDA

View Full Version : Left align the items on Zommout



sijithjames
8th December 2017, 11:51
When doing zoomIn, zoom happens on left aligned (item on top left is always visible) but on zoom out items going to the center of the view by leaving lot of space on left,

I want all items should be left aligned on zoom out also.

Code for LeftAigned ZoomIn.


void csGuiView::wheelEvent(QWheelEvent *event)
{

if ((event->modifiers()&Qt::ControlModifier) == Qt::ControlModifier
&& event->angleDelta().x() == 0)
{
QPoint pos = event->pos();
QPointF posf = this->mapToScene(pos);

double angle = event->angleDelta().y();

double scalingFactor;

if(angle > 0)
{
scalingFactor = 1 + ( angle / 360 * 0.1);
}else if (angle < 0)
{
scalingFactor = 1 - ( -angle / 360 * 0.1);
} else
{
scalingFactor = 1;
}

m_pvtData->m_scale = scalingFactor;

this->scale(scalingFactor, scalingFactor);

double w = this->viewport()->width();
double h = this->viewport()->height();

double wf = this->mapToScene(QPoint(w-1, 0)).x()
- this->mapToScene(QPoint(0,0)).x();
double hf = this->mapToScene(QPoint(0, h-1)).y()
- this->mapToScene(QPoint(0,0)).y();

double lf = posf.x() - pos.x() * wf / w;
double tf = posf.y() - pos.y() * hf / h;

/* try to set viewport properly */
this->ensureVisible(lf, tf, wf, hf);


QPointF newPos = this->mapToScene(pos);


this->ensureVisible(QRectF(QPointF(lf, tf) - newPos + posf,
QSizeF(wf, hf)), 0, 0);

}

if ((event->modifiers()&Qt::ControlModifier) != Qt::ControlModifier) {
QGraphicsView::wheelEvent(event);
}

event->accept();
}

high_flyer
8th December 2017, 22:58
I am not sure about this (years since I have done anything with QGraphicsView - but I'd try setting the view alignment to top left:
http://doc.qt.io/qt-5/qgraphicsview.html#alignment-prop

If this does not help, I'd experiment with the viewport anchor:
http://doc.qt.io/qt-5/qgraphicsview.html#transformationAnchor-prop

Please share here if you have solved it and how.