缩放QGrahicsView时,mousewheentturn首先滚动,然后缩放
Scaling a QGrahicsView when MouseWheenTurned first scrolls, then scales
我试图添加放大/缩小功能到我在Qt绘图的图形。我首先做的是扩展QGraphicsScene与我自己的类GraphicsScene和重载车轮事件。
class GraphicsScene : public QGraphicsScene
{
Q_OBJECT
public:
GraphicsScene(QObject *parent, bool drawAxes){ /*drawing stuff here.. */}
virtual void wheelEvent(QGraphicsSceneWheelEvent *mouseEvent);
signals:
void mouseWheelTurned(int);
};
void GraphicsScene::wheelEvent(QGraphicsSceneWheelEvent* mouseEvent) {
int numDegrees = mouseEvent->delta() / 8;
int numSteps = numDegrees / 15; // see QWheelEvent documentation
emit mouseWheelTurned(numSteps);
}
当车轮转动时,一个事件被发送到包含场景的视图,并在那里执行缩放:
class GraphicsView : public QGraphicsView{
Q_OBJECT
qreal m_currentScale;
public:
GraphicsView(QWidget * parent): QGraphicsView(parent){ m_currentScale = 1.0; }
public slots:
void onMouseWheelTurned (int);
};
void GraphicsView::onMouseWheelTurned(int steps) {
qreal sign = steps>0?1:-1;
qreal current = sign* pow(0.05, abs(steps));
if(m_currentScale+current > 0){
m_currentScale += current;
QMatrix matrix;
matrix.scale(m_currentScale, m_currentScale);
this->setMatrix(matrix);
}
}
这是有效的,但我注意到,如果我放大很多,例如到图形的顶部,那么图形不再完全在视口中,然后我缩小,程序首先滚动到图形的底部。我可以看到垂直滚动条向下滑动。只有当它到达底部时,它才开始缩小。有什么问题吗?
我想在没有上下滚动的情况下放大/缩小
你最好在场景中处理这个问题,并向视图发送一个事件。
直接在视图中使用QGraphicsView::wheelEvent}处理事件,然后调用其缩放函数
相关文章:
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 计算缩放多边形的比例,得到给定的多边形面积
- QwtPlot具有相等的轴和自动缩放
- 如何在directx/c++中进行平移/缩放操作
- 在Qt中实现无限可缩放的画布
- 是否可以为 QPixmap 派生类嵌入缩放方法?
- 形状对象的旋转和缩放不正确C++
- 如何在 OpenSceneGraph 中缩放/旋转/移动资产
- 如何在OpenGL(GLFW,很高兴)中进行2D缩放?
- 改变或缩放两个正态分布以具有特定的相关系数
- SDL2 调整窗口大小后如何缩放鼠标坐标?
- 如何在OpenGL中正确旋转和缩放对象?
- 仅当类型为 std::complex 时,才进行缩放
- 将 VS Code 用于跨平台可缩放C++项目
- 根据帧速率缩放/缩小数字
- 在 c++ 中缩放浮点值
- 如何防止 DirectX C++程序的拉伸/缩放
- 我在QChartView中找不到缩放图形的鼠标滚轮滚动事件
- 根据其大小缩放滚动球控件
- 缩放QGrahicsView时,mousewheentturn首先滚动,然后缩放