I've created an application with Qt Creator (OS Ubuntu 13.04). One function creates a window and draws a graphic using GLUT library, picture is right. But when I try to close window and continue working with my program, it terminates. How can I avoid this?
There is the code of my function:
void plot(int argc, char**argv,.../*other arguments*/)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA);
glutCreateWindow("Green Window");
//some code
//...
glutDisplayFunc( draw );
glutMainLoop();
}
Application output prints "... exited with code 0"
If you read e.g. this reference of glutMainLoop
you will see that glutMainLoop
never returns. That means it will call exit
directly instead of returning.
If you're using Qt, then it's able to open windows containing OpenGL contexts, windows compatible with the rest of Qt and which you can close at will.
You might want to switch to freeglut which has implemented the function glutLeaveMainLoop(). As the documentation says:
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.
Source: http://freeglut.sourceforge.net/docs/api.php
For FreeGlut. If You want to close only window which has created with GLUT. Look:
int handle = glutCreateWindow("Red square example");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
No thanks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With