我有以下类,它继承了QGraphicsView:
class CurveGraphicView : public QGraphicsView {
Q_OBJECT
protected:
virtual void resizeEvent( QResizeEvent* event ) override;
virtual void wheelEvent(QWheelEvent* event) override;
};
我必须在wheelEvent中跟随代码:
void CurveGraphicView::wheelEvent( QWheelEvent* event ) {
float zoomInFactor = 1.25;
float zoomOutFactor = 1 / zoomInFactor;
float zoomFactor = zoomOutFactor;
if(event->angleDelta().y() > 0){
zoomFactor = zoomInFactor;
}
scale(zoomFactor, zoomFactor);
}
正如我所看到的,我正在使用wheel事件来放大/缩小视图内部。
虽然我在移动鼠标并同时使用鼠标滚轮时未调用此功能,但效果很好。
以下代码有效:
int main( int argv, char** args) {
QApplication qtApp(argv, args);
CurveGraphicView view;
view.show();
return qtApp.exec();
}
经过一些研究,我认为最终孤立了这个问题,但我仍然没有解决它:
CurveGraphicView父Widget还包含一个QOpenGLWidget。当我删除此QOpenGLWidget时,问题不再出现:
class Curve2DOpenGLWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core {
Q_OBJECT
public:
Curve2DOpenGLWidget( QWidget* parent = nullptr,
const QString& xLabel = "x",
const QString& yLabel = "y",
const QString& zLabel = "z" );
~Curve2DOpenGLWidget();
void CheckGLError();
void initializeGL() override;
void resizeGL(int width, int height) override;
//Function mixing QPainter Drawings and Native OpenGL calls
void paintGL() override;
//Native OpenGL calls
void NativePaintGL();
protected:
void keyPressEvent(QKeyEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
};
有什么想法吗?
编辑:
我以更清晰的方式转发了这个主题: Qt QOpenGLWidget wheelEvent strange behaviour