我正在使用 Qt4 在 OpenGL 中制作一个简单的三角形,它工作正常,直到我使用设置格式来启用多重采样。这是我的代码:
#include <QApplication>
#include <QtOpenGL>
// gl window class
class GLWindow : public QGLWidget
{
public:
GLWindow(QWidget *parent = nullptr)
: QGLWidget(parent){}
protected:
// ALL THE FOLLOWING FUNCTIONS ARE OVERRIDDEN FROM QGLWIDGET
void initializeGL()
{
QGLFormat newFormat = this->format();
newFormat.setSampleBuffers(true);
newFormat.setSamples(16);
this->setFormat(newFormat);
}
void resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1, 1, -1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(0, 1);
glColor3f(0, 1, 0);
glVertex2f(1, -1);
glColor3f(0, 0, 1);
glVertex2f(-1, -1);
glEnd();
}
};
// main function
int main(int argc, char **argv)
{
QApplication app(argc, argv);
GLWindow window;
window.resize(640, 480);
window.show();
return app.exec();
}
在我在“initlializeGL”中添加格式内容之前,它工作正常(显然除了没有多重采样)。
然后,我添加格式的东西,窗口没有关闭。当我这么说的时候,我的意思是当我按下右上角的“X”按钮时它不会关闭,或者当我调用窗口的“close()”函数时它甚至不会关闭。
此外,当您按下“X”按钮(我检查过)时,它会调用“closeEvent()”,但实际上没有关闭。我尝试在覆盖的“closeEvent()”函数中调用“close()”,但它什么也没做。
再一次,我删除了“initializeGL()”中的代码,然后它就正常关闭了。因此,我尝试将“initializeGL()”中的代码移动到构造函数中。多重采样有效,当我按“X”时它会关闭。伟大的!除了我在窗口关闭后得到这个:
就是这样。简而言之:
- 当“initializeGL()”中没有“setFormat()”相关代码时,一切正常。
- 当我将“setFormat()”相关代码放入“initializeGL()”时,窗口不会关闭。
- 当我将“setFormat()”相关代码放入构造函数中时,窗口关闭时出现上图中显示的奇怪错误;
那么如何让窗口关闭,同时保持启用多重采样并且在窗口关闭后不会出现一些愚蠢的错误?
编辑:这是我的 .pro 文件中的文本
QT += core
QT += gui
QT += opengl
SOURCES += \
main.cpp