QGraphicsView setScale(qreal zoom)
-
Hi all!
I have a problem, now i writing a program, that uses QGraphicsView.
View has Zooming functionality@void MainView::scaleBy(double factor)
{
scale(factor, factor);
}void MainView::wheelEvent(QWheelEvent *event)
{
if (event->modifiers() == Qt::ControlModifier)
{
scaleBy(std::pow(4.0 / 3.0, (-event->delta() / 240.0)));
}
else
{
QGraphicsView::wheelEvent(event);
}
}void MainView::zoomIn()
{
scaleBy(1.1);
}void MainView::zoomOut()
{
scaleBy(1.0 / 1.1);
}@but I want to have an limited zoom, also a tool to set fixed scale (such 100%, 150%, etc). I can't implement a function like this, QGraphicsView::setScale(qreal zoom), because I need current scale value. Anybody can say me, how I can get it?
-
-
[quote author="chetankjain" date="1281335356"]i think u can have the original scale factor and current scale factor stored as variables. compute the current scale (100, 150, 200) always based on the original scale. if you don't do this, you might geometrically scale the image to a very high factor[/quote]
Look, I want insert check for minimum and maximum scale value to MainView::scaleBy(double factor),
@if (cur_value * factor > MAXIMUM)
{
cur_value = MAXIMUM;
setScale(MAXIMUM);
}
else
scale(factor, factor);@But I can't do this, because I haven't setScale(), and I also havn't cur_value. Not sure that store it as class-member is a good idea.
-
I solved my problem, but I had used storing scale value as class-member variable. If anyone knows more beauty way, please write it here.
@void MainView::scaleBy(double factor)
{
m_curScaleFactor *= factor;
scale(factor, factor);
}void MainView::scaleView(qreal scaleFactor)
{
if (((m_curScaleFactor == m_minScale) && (scaleFactor < 1.0)) ||
((m_curScaleFactor == m_maxScale) && (scaleFactor > 1.0))) return;qreal sc = scaleFactor; if ((m_curScaleFactor * sc < (m_minScale))&&(sc < 1.0)) { sc = m_minScale / m_curScaleFactor; } else if ((m_curScaleFactor * sc > m_maxScale)&&(sc > 1.0)) { sc = m_maxScale / m_curScaleFactor; } scaleBy(sc);
}
void MainView::setZoom(int percentZoom)
{
qreal targetScale = (qreal)percentZoom / 100.0;
qreal scaleFactor = targetScale / m_curScaleFactor;
scaleBy(scaleFactor);
}@ -
[quote author="chetankjain" date="1281338119"]for a graphicsview, i think you can get the current transform and then the m11 and m22 elements would specify the current horizontal and vertical scaling. the defaults would be 1.0. would this suffice ?
[/quote]thankyou! it works fine.
-
Is @scale()@ the method you need? The documentation suggests that it returns the value previously set by @setScale(...)@.
-
[quote author="Tobias Hunger" date="1281342135"]Is @scale()@ the method you need? The documentation suggests that it returns the value previously set by @setScale(...)@.[/quote]
QGraphicsView haven't whis method (4.6.2).
@void scale(qreal sx, qreal sy)@
Scales the current view transformation by (sx, sy). -
So, if it will be useful for anybody:
@
// in *.h
inline qreal curScale() const {return transform().m11();}// in *.cpp
void MainView::scaleBy(qreal scaleFactor)
{
qreal curScaleFactor = transform().m11();
if (((curScaleFactor == minScale) && (scaleFactor < 1.0)) ||
((curScaleFactor == maxScale) && (scaleFactor > 1.0))) return;qreal sc = scaleFactor; if ((curScaleFactor * sc < minScale)&&(sc < 1.0)) { sc = minScale / curScaleFactor; } else if ((curScaleFactor * sc > maxScale)&&(sc > 1.0)) { sc = maxScale / curScaleFactor; } scale(sc, sc);
}
void MainView::setZoom(int percentZoom)
{
qreal targetScale = (qreal)percentZoom / 100.0;
qreal scaleFactor = targetScale / transform().m11();
scaleBy(scaleFactor);
}@I used only m11, cause in my case m11 == m22.