Qt World Summit: Submit your Presentation

Mouse click event has a displacement in a QOpenGLWidget

  • Hi, I have an application that allows the user to click over some points and It returns its color. The problem is that when I click over a point, the coordinates that the mouse event returns are a bit moved and It returns the color of the background instead.
    I have a class that inhetirs from QOpenGlWidget that implements this Mouse event and this Widget is set in a UI with some buttons. Here you have an image:

    If I click some pixels around the point, it returns the right color of the point, so I think that the click is returning a position with is not exactly in my QOpenGlWindow, it has some displacement
    Here I show you what is happening:
    I click over a point and it returns white color:
    Then, I click over one point of the background near the point and it returns the color of the point. Note that this only happens in that right point, not all the points around my point.

    I think that the mouse event is returning a point considering also the margins of the window, not only the window. Any ideas?.


  • Lifetime Qt Champion

  • I've just tried it but I get the same trouble. Once I get the x and y values, I do glReadPixels to check the colour under this position. When the cursor is near the margins of the window I get the colour of the margins instead of white.
    0_1561652583348_color margen.png
    It occurs the same on top, left and right margins.
    I am using a layout to place the qopenglwidget on the ui.

  • Lifetime Qt Champion

    Where do you catch the mouse event?

  • @Christian-Ehrlicher in glwindow which is the class that inherits from QOpenGLWidget and creates the window.

     bool glwindow::eventFilter(QObject *obj, QEvent *event)

    and in my interface class I do this:

    QSurfaceFormat format;
        format.setVersion(4, 1);
        glwindow *ventana = new glwindow();
        std::cout<<"Ventana lanzada"<<std::endl;
        this->ventana = ventana;

  • Lifetime Qt Champion

    I'm not sure but do the corrdinates match to the main windgets coordinates? Did you check by e.g. overwriting mouseMoveEvent() on the main window?

  • @Christian-Ehrlicher mouseMoveEvent is a QWidget function, so what do you mean, the openGLWidget or the main window where the widget is placed?

Log in to reply