Qt and GL Concept Questions

I am a novice with both. My task is to write code for a 2D display, a strip chart. No opacity, color fades, hidden anything, etc. Not even any curves. From the glprogramming.com web site and the on line book I find example code of this basic format:

int main( …)
{
glutInit(…);
… // additional code to create and set up
glutMainLoop();     // the app stays here until done
return 0;
}

From my Qt book the basic format is:

int main(…)
{
QApplication app( argc, argv );
… // additional code to create and set up
int status = app.exec();    // the app stays here until done
return status;
}

I am certainly not the first to notice but the Qt and GL concepts are mutually exclusive.

The Qt part of this app must be able to accept user input at any time and send that input into the constantly running GL app. With what little I have accomplished so far, it looks like it is possible, but I don’t understand how. The GL segment of this app must continuously ingest data from outside the application. I have not gotten that far yet.

Do you know of any web page or book where someone has taken the time to explain this?

My current perspective has the GL environment within the Qt environment. Does that seem appropriate and what changes need to be made to the GL main loop to accommodate its being inside the Qt environment rather than running directly within main()?

I am writing test code now but even small bits of information may affect my trajectory.

Thank you for your time

The “GL concept” is actually GLUT rather than OpenGL per se. GLUT is a GUI toolkit, somewhat like Qt but much simpler. It was created specifically to make the code in the “red book” (formally, “The OpenGL Programming Language”) simple and portable. Otherwise, a 20-line test program would need another hundred or so lines of boilerplate initialisation and window-management code which would need to be repeated for each operating system.

As you observe, Qt and GLUT are mutually exclusive. They both do the same thing and if you try to use both they’ll “fight” for overall control of the program. Fortunately, it’s fairly straightforward to take code which was written to use GLUT and convert it to use Qt instead. The main conversions are:

[ul]
[li] glutDisplayFunc -> paintGL
[/li][li] glutReshapeFunc -> resizeGL
[/li][li] glutMotionFunc, glutPassiveMotionFunc -> mouseMoveEvent
[/li][li] glutMouseFunc -> mousePressEvent, mouseReleaseEvent
[/li][li] glutKeyboardFunc, glutSpecialFunc -> keyPressEvent
[/li][li] glutKeyboardUpFunc, glutSpecialUpFunc -> keyReleaseEvent
[/li][li] glutEntryFunc -> enterEvent, leaveEvent
[/li][li] glutVisibilityFunc -> showEvent, hideEvent
[/li][/ul]
In each case, rather than registering a callback function via the relevant glutWhateverFunc() function, you override the corresponding method of the QGLWidget class.

Window settings passed to glutInitDisplayMode() and/or glutInitDisplayString() are instead implemented using the methods of the QGLFormat class. You’ll probably want to call setAttribute(Qt::WA_OpaquePaintEvent) on the widget so that you don’t get flicker from Qt clearing the window each time.

Replace glutPostRedisplay() with the update() method on the QGLWidget. Replace glutTimerFunc() and glutIdleFunc() with QTimer. For glutGetModifiers(), use the modifiers() method on the event (that function should only be called from within mouse/key event handlers which take the event as a parameter).

If you need GLUT’s text functions or geometric primitives, I’ve been able to safely mix FreeGLUT with Qt provided that you call glutInit() first (and don’t use any window or event-handling functions which would conflict with Qt). However, those probably won’t work with a core profile context (this isn’t an issue if you’re using OpenGL 2.1, though).

Since the OP I found a stackoverflow thread that states glut does not provide anything that is not already provided by Qt and OpenGL. As it stands now I have the QT widget with a Qt layout that contains several widgets and a QGLWidget for the openGL display. A few buttons in the Qt are working and the GL code has a paintGL() and a resizeGL() that are overrides of what I think are original functions in openGL. They are called by the resizeing operation so I think the two applications (Qt and openGL) are playing together well. I probably will not know for sure until I add in more SLOTs and SIGNALS to implement control.

For now I am following another thread here and working on implementing the basic plotting code. I’ll start with a basic sawtooth or triangle function, or maybe sin/cos and develop the ability to plot points before working on ingesting data from another app.

Thanks for the time you took to read my question and post a reply.