How to close GLUT window without terminating of application?
Asked Answered
S

3

5

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"

Scrawl answered 31/7, 2013 at 13:49 Comment(3)
Is this a Qt GUI project? Or are you simply using Qt Creator to make a basic C++ project?Renvoi
Yes, I create the main window with Qt GUI.Scrawl
In that case, like @JoachimPileborg mentioned, since glutMainLoop doesn't return, you should use Qt's builtin support for OpenGL rendering.Renvoi
T
3

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.

Trivium answered 31/7, 2013 at 13:52 Comment(0)
C
4

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

Callosity answered 13/9, 2013 at 9:32 Comment(0)
T
3

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.

Trivium answered 31/7, 2013 at 13:52 Comment(0)
N
2

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.

Naphthyl answered 7/7, 2018 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.