5

我用 Qt Creator (OS Ubuntu 13.04) 创建了一个应用程序。一个函数使用 GLUT 库创建一个窗口并绘制图形,图片是正确的。但是当我尝试关闭窗口并继续使用我的程序时,它会终止。我怎样才能避免这种情况?

有我的功能的代码:

void plot(int argc, char**argv,.../*other arguments*/)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA);
    glutCreateWindow("Green Window");
    //some code
    //...
    glutDisplayFunc( draw );
    glutMainLoop();
}        

应用程序输出打印“... exited with code 0”

4

3 回答 3

3

您可能想要切换到实现了函数 glutLeaveMainLoop() 的 freeglut。正如文档所说:

The glutLeaveMainLoop function causes freeglut to stop its event loop.

Usage

void glutLeaveMainLoop ( void );

Description

The glutLeaveMainLoop function causes freeglut to stop the event loop. If the GLUT_ACTION_ON_WINDOW_CLOSE option has been set to GLUT_ACTION_CONTINUE_EXECUTION, control will return to the function which called glutMainLoop; otherwise the application will exit.

If the application has two nested calls to glutMainLoop and calls glutLeaveMainLoop, the behaviour of freeglut is undefined. It may leave only the inner nested loop or it may leave both loops. If the reader has a strong preference for one behaviour over the other he should contact the freeglut Programming Consortium and ask for the code to be fixed.

Changes From GLUT

GLUT does not include this function.

来源: http: //freeglut.sourceforge.net/docs/api.php

于 2013-09-13T09:32:27.463 回答
3

如果您阅读例如此参考资料,glutMainLoop您将看到它glutMainLoop永远不会返回。这意味着它将exit直接调用而不是返回。

如果您使用的是 Qt,那么它能够打开包含 OpenGL 上下文的窗口、与 Qt 的其余部分兼容的窗口,并且您可以随意关闭这些窗口。

于 2013-07-31T13:52:54.550 回答
2

对于 FreeGlut。如果您只想关闭使用 GLUT 创建的窗口。看:

int handle = glutCreateWindow("Red square example");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);

不,谢谢。

于 2018-07-07T19:16:21.643 回答