2013-07-31 112 views
5

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

应用程序的输出打印” ......与代码0" 退出

+0

这是一个Qt GUI项目?或者你只是使用Qt Creator来创建一个基本的C++项目? –

+0

是的,我用Qt GUI创建主窗口。 –

+0

在这种情况下,就像@JoachimPileborg提到的那样,由于'glutMainLoop'不会返回,所以应该使用Qt内置的OpenGL渲染支持。 –

回答

1

如果你读如this reference of glutMainLoop你会看到glutMainLoop永远不会返回。这意味着它将直接呼叫exit而不是返回。

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

2

您可能想切换到已实施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

相关问题