Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help please, implementing zoom using mouse wheel but QGLWidget does not update.



  • How can I make the QGLWidget updated when I use mouse wheel for zoom??

    I really need your help on my problem. I am implementing a zoom function using wheelEvent(). In the wheelEvent(), I compute a zoom factor, then the paintGL() function uses the zoom factor to determine the parameter for glOrtho to display the zoomed image.

    In the paintGL(), I can manually change m_range to say 100, 200, 50, and the QGLWidget shows the redrawed image as expected. Or when I resize the window, the QGLWidget draws the zoomed image properly.

        glOrtho( -m_range , m_range, -m_range, m_range, m_range, -m_range );
    

    However, when m_range is set by the program (updating m_range using the zoom factor from wheelEvent()), the QGLWidget only display the initial image and does not change when I use mouse wheel to zoom. The wheelEvent() is entered because I saw the debug message.

    How can I make the QGLWidget update when I use mouse wheel?? I want it to be changed whenever I use mouse wheel. Thank you.

    PS. My program is to display 3D points. But, I this QT example program to experiment the zoom function.

    @
    void Lines::wheelEvent(QWheelEvent *event)
    {
    float delta = (float)event->delta();

        m_zoom += (delta/120.0);
    
        updateGL();
    }
    
    
    void Lines::paintGL()
    {
        printf("Lines::paintGL()\n");
    
        setViewFrustum();
    
        // Clear the buffer with the current clearing color
        glClear( GL_COLOR_BUFFER_BIT );
     
        // Set drawing colour to red
        glColor3f( 1.0f, 0.0f, 0.0f );
    
       ....
    }
    
    
    void Lines::setViewFrustum()
    {
        double max_range = 300.0;
        double min_range = 50.0;
    
        m_range += m_zoom;
    
        if (m_range > max_range)
            m_range = max_range;
    
        if (m_range < min_range)
            m_range = min_range;
    
        glOrtho( -m_range , m_range, -m_range, m_range, m_range, -m_range );
    
        // DEBUG
        FILE * f = fopen&#40;"Debug.txt", "a+"&#41;;
        fprintf(f, "zoom %f, left %lf, right %lf\n", m_zoom, -m_range, m_range&#41;;
        fclose(f);
    
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
    
    }
    

    @

    I cannot post the full sourcecode. It is larger than the limit. Let me know if you need more information.

    Thank you very much.



  • For what I can see glOrtho() is changing the model view matrix and you set it to identity again calling glLoadIdentity() at the end of setViewFrustrum() function, and therefore you can't see any change.

    Anyways you should use glOrtho() to change the perspective matrix:
    @void Lines::setViewFrustum()
    {
    double max_range = 300.0;
    double min_range = 50.0;

    m_range += m_zoom;

    if (m_range > max_range)
    m_range = max_range;

    if (m_range < min_range)
    m_range = min_range;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( -m_range , m_range, -m_range, m_range, m_range, -m_range );

    // DEBUG
    FILE * f = fopen("Debug.txt", "a+");
    fprintf(f, "zoom %f, left %lf, right %lf\n", m_zoom, -m_range, m_range);
    fclose(f);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    }@

    I hope it helps,
    H.



  • Hi Arnaut, Thank you very much for your help. Now it's work!


Log in to reply