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

Best way to redraw or repaint a QOpenGLWidget for Qt5 c++



  • Hi,

    I am having some issues with wrapping my head around the best way to repaint or redraw my QOpenGLWidget in my mainwindow.

    As of now I want to move a certain "target" object around my QOpenGLWidget by manipulating the (x, y) coordinates. Every time I change the targets coordinates I would like to redraw where the target is and that is it. In my current program I am working on I use the update() function but it doesn't seem to be consistent in the redraw and only will redraw once.

    What is the best, most efficient way to redraw my widget multiple times in my mainwindow.

    Thanks!


  • Lifetime Qt Champion

    Hi,

    From the looks of it the Hello GL2 example might be of interest.



  • @SGaist Thank you! This helps a lot!



  • @SGaist I was just wondering, I noticed that when I hook up my debugger to the Hello GL2 example.. It looks like the paintGL() function is called or triggered when a mouse even occurs over the slider widget.

    I am assuming it is called when either of these functions signal is released. Is there a way to trigger the paintGL() function with a Qtimer? Or do you know of an example I can look at?

    void GLWidget::mousePressEvent(QMouseEvent *event)
    {
        m_lastPos = event->pos();
    }
    
    void GLWidget::mouseMoveEvent(QMouseEvent *event)
    {
        int dx = event->x() - m_lastPos.x();
        int dy = event->y() - m_lastPos.y();
    
        if (event->buttons() & Qt::LeftButton) {
            setXRotation(m_xRot + 8 * dy);
            setYRotation(m_yRot + 8 * dx);
        } else if (event->buttons() & Qt::RightButton) {
            setXRotation(m_xRot + 8 * dy);
            setZRotation(m_zRot + 8 * dx);
        }
        m_lastPos = event->pos();
    }
    
    void Window::keyPressEvent(QKeyEvent *e)
    {
        if (e->key() == Qt::Key_Escape)
            close();
        else
            QWidget::keyPressEvent(e);
    }
    

  • Lifetime Qt Champion

    IIRC, you can connect the QTimer to the update slot of your widget.

    It's not the mouse itself. When you move a slider, the corresponding properties of the GLWidget object are changed and a call to update is done at the end of the slot.


Log in to reply