GraphicsView缩小(ZoomOut)规模问题
问题描述:
我的工作环境:Qt的5.8 MSVC2015 64位,QT GraphicsView,Windows 7的64位GraphicsView缩小(ZoomOut)规模问题
当GraphicsView垂直滚动条消失,缩小应该停止。
所以,我想下面的代码,但它未能奏效:
void GraphicsView::scale(qreal scaleFactor)
{
QRectF r(0, 0, 1, 1); // A reference
int pos_x = this->horizontalScrollBar()->value();
int pos_y = this->verticalScrollBar()->value();
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(r).width(); // absolute zoom factor
if (factor > 7) { // Check zoom out limit
return;
}
//Failed, this code failed If zoom out again.**
if(pos_x <= 0 && pos_y <= 0)
{
return;
}
任何建议,我该怎么做才能解决上面的代码?
答
没有回答我的问题。这里是我解决方案, 从wheelEvent检查我们正在放大或缩小。我比例检查垂直滚动条&。
这里_steps是我的类GraphicsView的私有数据成员。从QGraphicsView派生的GraphicsView。
void GraphicsView::wheelEvent(QWheelEvent * event)
{
// Typical Calculations (Ref Qt Doc)
const int degrees = event->delta()/8;
_steps = degrees/15; // _steps = 1 for Zoom in, _steps = -1 for Zoom out.
}
void GraphicsView::scale(qreal scaleFactor)
{
QRectF r(0, 0, 1, 1); // A reference
int pos_x = this->horizontalScrollBar()->value();
int pos_y = this->verticalScrollBar()->value();
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(r).width(); // absolute zoom factor
if (factor > 7) { // Calculate your zoom in limit from factor
return;
}
//When there is no scroll bar, am I still I am zooming, stop it using _steps
if(pos_x <= 0 && pos_y <= 0 && _steps == -1)
{
return;
}
QGraphicsView::scale(scaleFactor, scaleFactor);
}
我知道有更好的解决方案,然后这个,但是我发现这只是:(