首页 > 解决方案 > 将鼠标悬停在 OpenGL 绘制的对象上并显示对象的变量时更改光标图像

问题描述

我目前正在开发一个在 64 位操作系统 Windows 10 Pro 上使用 C++、OpenGL、Qt 5.9.2 和 Microsoft Visual Studio Professional 2015 的项目。

我有一个我创建的用户界面,在该用户界面中,有一个 QGLWidget,我用于绘制过程,以及其他小部件,如按钮和 dockWidget。我有一个 Ball 类,它有变量(距离(double)和角度(int)),用于确定该 Ball 的实例将在 QGLWidget 内绘制的位置。Ball 类还有 2 个变量,即 id(int)、model(String) 和 year(int) 除了 Ball 绘图之外,所有其他绘制过程都会绘制线条。

图纸是二维的。

每个球都有相同的颜色(rgb)!

第一个问题:我想左键单击其中一个 Ball 实例,我想在 DockWidget 上显示它的 ID、型号和年份。

第二个问题:在做我在第一个问题部分提到的事情时。我希望光标图像在悬停在任何 Ball 实例上方时更改,并在不更改时更改回默认的 Windows 鼠标光标。

我创建了一个检查 MouseEvent 是否为 LeftClick 的函数:

void DisplayManager::mousePressEvent(QMouseEvent* ev) { 

    if (ev->buttons() & Qt::LeftButton) { // Balls Are Green
        if(// CHECK IF THERE IS A BALL AT THE CLICKED COORDINATES) {
            // DISPLAY THE X and Y OF THE BALL AT THE DOCK WIDGET
        }
    }
}

这是我的 initializeGL 函数:(DisplayManager 是我的 QGLWidget 的名称

void DisplayManager::initializeGL() {
    glEnable(GL_COLOR_MATERIAL); // Enables the changing of the draw color with glColor() functions
    glColor3f(0.0, 1.0, 0.0);

    glEnable(GL_DEPTH_TEST);
    glClearColor(0, 0, 0, 1); //sets a black background 1 0 0 1
}

在此基础上,这是一个挑选问题,互联网上有一些关于它的信息,但我没有使用 GLUT,也没有使用任何着色器。因此,鉴于所有这些,我无法找到任何有效的解决方案或线索来说明我如何才能完成我想要的所有事情。

如果有人可以帮助我解决这些问题中的至少一个,我会非常高兴。

标签: c++qtopengl

解决方案


我目前已经完成了该项目的工作。我认为我应该回答我的问题,以防将来遇到类似问题的人遇到我的问题。

void DisplayManager::mousePressEvent(QMouseEvent* ev) {

    // The processes that are being executed after a left mouse button click on the DisplayManager.
    if (ev->buttons() & Qt::LeftButton) {
        double aspectRatio = openGLWidgetWidth / openGLWidgetHeight;
        int xOfClicked = ev->x() - (openGLWidgetWidth / 2);
        int yOfClicked = - (ev->y() - (openGLWidgetHeight / 2));

        // The variable to be used for calculating fault tolerance of the click event.
        int distanceBetweenPressAndDraw = 0;

        // Executes the processes inside for every ball in vector.
        for (int i = 0; i < ballVector.length(); i++) {

            // Calculates the screen coordinates of the i'th ball.
            int rangeOfBallInScreenDistance = rangeToScreenDistance(ballVector.at(i).range);
            double screenXOfBall = rangeOfBallInScreenDistance * sin(ballVector.at(i).degree * DEGTORAD);
            double screenYOfBall = rangeOfBallInScreenDistance * cos(ballVector.at(i).degree * DEGTORAD);

            // Calculates the distance between pressed position and the i'th ball according to the screen coordinates.
            distanceBetweenPressAndDraw = sqrt(pow((screenXOfBall - xOfClicked), 2) + pow((screenYOfBall - yOfClicked), 2));

            // Decides if the clicked position is a ball (considering the fault tolerance).
            if (distanceBetweenPressAndDraw < 10) {
                emit printXY(QPointF(xOfClicked, yOfClicked)); // Prints the screen coordinates of the clicked positions (At the Dock Widget inside Main Window).
            }
        }
    }
}

这是我的第一个问题的解决方案。如果有人可以在评论中回答我的第二个问题或以某种方式回答,我会很高兴。


推荐阅读