QGLWidget详解

发布时间:2023-05-20

一、QGLWidget概述

QGLWidget是Qt中的一个基于OpenGL的小部件。它是一个用于显示3D图形和动画的窗口类,是一种可以在Qt框架中进行OpenGL编程的理想方式。比起使用OpenGL直接开发3D应用程序,QGLWidget通过提供QWidget的最基本功能以及其他附加功能,比如显示模式和帧速率控制,简化了开发工作。它是基于C++语言编写,同样可以支持Python、Java等编程语言。

二、QGLWidget的创建与使用

使用Qt进行OpenGL编程可以利用QGLWidget这个类。要使用它,在新建QT程序界面中,选择Qt Widgets Application,然后在类名中填写QGLWidget,选择继承 QDialog 即可创建一个QGLWidget的子类。

1、窗口属性设置

在构造函数中,我们需要设置窗口的各种属性。像颜色、几何形状、可调整的大小等属性。如下代码:

QGLWidget::QGLWidget(QWidget *parent) :
   QGLWidget(parent)
{
    // Set the background color to black
    this->setBackgroundColor(QColor(0, 0, 0, 255));
    // Set the default window size
    this->resize(800, 600);
    // Enable auto-refreshing
    this->setAutoBufferSwap(true);
    // Set the window title
    this->setWindowTitle("QGLWidget");
    // Make the window be able to receive keyboard events
    this->setFocusPolicy(Qt::StrongFocus);
    // Turn off anti-aliasing
    this->setAutoFillBackground(false);
    this->setAttribute(Qt::WA_NoSystemBackground, true);
}

2、OpenGL上下文

在OpenGL中,我们需要创建上下文环境(context)来使OpenGL的操作与应用程序窗口上下文进行交互。QGLWidget的构造函数会自动创建一个OpenGL的上下文环境供我们使用。

3、渲染器

在QGLWidget中,我们需要定义渲染器来绘制3D图像。每个QGLWidget子类都必须实现渲染器以显示图像。下面示例的代码中showEvent()是一个继承自父QDialog的函数,会在QGLWidget控件显示时调用对应方法。

void GLWidget::showEvent(QShowEvent *)
{
    // Start the timer to refresh the GLView
    this->startTimer(16);
    // Call initializeGL() to setup the OpenGL environment
    this->initializeGL();
    // Call paintGL() to render the scene
    this->paintGL();
}
void GLWidget::initializeGL()
{
    // Set the clear color to black
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    // Enable depth testing
    glEnable(GL_DEPTH_TEST);
    // Enable back-face culling
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
}
void GLWidget::paintGL()
{
    // Clear the color and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    // Render the scene
    renderScene();
    // Swap the front and back buffers
    this->swapBuffers();
}

4、鼠标交互控制

鼠标交互控制可以让用户通过鼠标控制3D场景的方向和位置。在QGLWidget中,我们可以通过重载鼠标事件函数来实现鼠标控制。下面示例代码中,我们使用了QMouseEventmouseMoveEvent函数来实现鼠标移动后通过计算相机位置和转换矩阵的方式实现3D场景的平移和旋转。

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    // Calculate the rotation amount based on the current and previous mouse positions
    float deltaX = (event->pos().x() - this->prevMouseX) * (this->rotationSpeed / 100.0f);
    float deltaY = (event->pos().y() - this->prevMouseY) * (this->rotationSpeed / 100.0f);
    // Update the camera rotation based on the calculated amount
    this->cameraRotation.setX(this->cameraRotation.x() + deltaX);
    this->cameraRotation.setY(this->cameraRotation.y() - deltaY);
    // Update the previous mouse position
    this->prevMouseX = event->pos().x();
    this->prevMouseY = event->pos().y();
    // Recalculate the view matrix
    this->updateViewMatrix();
}
void GLWidget::mousePressEvent(QMouseEvent *event)
{
    // Store the current mouse position
    this->prevMouseX = event->pos().x();
    this->prevMouseY = event->pos().y();
}

5、键盘交互控制

除了鼠标控制外,我们还可以使用键盘方式对3D场景进行交互控制。在QGLWidget中,可以使用键鼠事件来实现键盘控制。下面代码中,我们重载了QKeyEventkeyPressEvent函数,根据用户输入的不同按键对应修改具体的场景参数。

void GLWidget::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Left:
        // Move the light to the left
        this->lightPos.setX(this->lightPos.x() - 0.1f);
        break;
    case Qt::Key_Right:
        // Move the light to the right
        this->lightPos.setX(this->lightPos.x() + 0.1f);
        break;
    case Qt::Key_Up:
        // Move the light up
        this->lightPos.setY(this->lightPos.y() + 0.1f);
        break;
    case Qt::Key_Down:
        // Move the light down
        this->lightPos.setY(this->lightPos.y() - 0.1f);
        break;
    default:
        QGLWidget::keyPressEvent(event);
        break;
    }
}

三、QGLWidget常见问题解决方案

1、模型显示问题

QGLWidget的坐标系统和OpenGL的坐标系统不同,从而在绘制模型的时候可能会出现变形或不能正常显示等问题。通常我们使用模型矩阵,视图矩阵和投影矩阵来构建OpenGL的坐标系统。下面代码展示如何通过构建转换矩阵来解决模型显示问题。

void GLWidget::renderScene()
{
    // Reset the model matrix
    this->modelMatrix.setToIdentity();
    // Translate the model to the center of the view
    this->modelMatrix.translate(QVector3D(-0.5f, -0.5f, -0.5f));
    // Update the model-view matrix
    this->updateModelViewMatrix();
    // Draw the model
    drawModel();
}

2、帧速率控制

在QGLWidget中,我们可以使用QTimeQTimer等方式来限制帧速率。通常情况下,帧速率控制可以减少CPU和GPU负载,避免不必要的性能开销。下面代码展示如何通过控制帧数来限制帧速率。

void GLWidget::timerEvent(QTimerEvent *)
{
    // Update the scene
    this->update();
    // Calculate the elapsed time since the last frame
    int msSinceLastFrame = this->frameTimer.elapsed();
    // Wait if the elapsed time is less than the desired frame time
    if (msSinceLastFrame < this->desiredFrameTime) {
        QThread::msleep(this->desiredFrameTime - msSinceLastFrame);
    }
    // Restart the frame timer
    this->frameTimer.start();
}

3、OpenGL版本问题

当使用QGLWidget进行OpenGL编程时,为了能够兼容多个显示卡和操作系统,我们需要判断OpenGL支持的版本并做出相应的调整。下面代码展示如何使用QGLFormat::setVersion()来设置OpenGL版本。

void GLWidget::initializeGL()
{
    // Specify the OpenGL version information
    QGLFormat glFormat = QGLFormat::defaultFormat();
    glFormat.setVersion(4, 4);
    glFormat.setProfile(QGLFormat::CoreProfile);
    glFormat.setSampleBuffers(true);
    // Set the format of the GLWidget
    this->setFormat(glFormat);
    // ...
}

四、总结

QGLWidget作为一个基于OpenGL的小部件,能够在Qt框架中进行OpenGL编程。通过QGLWidget的创建和使用,我们可以在Qt中实现3D图像和动画的显示。同时,QGLWidget也提供了一些常见问题的解决方案,如模型显示问题、帧速率控制和OpenGL版本问题。