Run Qt on a thread and draw it in Glut
I'm trying o run Qt on a thread. In my non Qt App, I don't control the main loop. And I want to use Qt. So I render my widget in a QImage and then my app draw its content. I use glut to build working example of what I want to do.
int g_width = 400;
int g_height = 400;
class QtApp : public QThread
int argc = 0;
char** argv = NULL;
app = new QApplication(argc,argv);
bouton = new QPushButton("PushMe");
image = new QImage(g_width,g_height,QImage::Format_ARGB32);
painter= new QPainter(image);
//glTexImage2D(GL_TEXTURE_2D,0 , GL_RGBA, g_width, g_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, qtapp->getImage());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
void reshape(int w, int h)
glViewport(0, 0, w, h);
if (h == 0)h = 1;
gluOrtho2D(0, 1, 0, 1);
int main(int argc, char** argv)
qtapp = new QtApp();
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glTexImage2D(GL_TEXTURE_2D,0 , GL_RGBA, g_width, g_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, qtapp->getImage());
When I called qtapp->getImage() line 48 and 88 that's render my QPushButton in a QImage, then I send the data to OpenGL to draw it. If I made the call in the main function all is working but if I uncomment the line 48 I get a segfault localize line 27.
Do you think this is due to Qt ? I can look at the source code of glut but I don't know what it could possibly do to interfere whit Qt.
Thanks to reading me.
For simple widget rendering you don't need QApplication::exec(), here is an example:
int main(int argc, char **argv)
QApplication app(argc, argv);
QPushButton button("test"); button.resize(button.sizeHint()); QImage image(button.rect().size(), QImage::Format_RGB32); image.fill(0xffffffff); QPainter p(& image); button.render(&p); p.end(); image.save("image.png"); return 0;
If you do need input event processing etc from Qt as well, you might still be able to do it in a single thread. What you'd do is use glutIdleFunc() to register a function for GLUT to call when the GLUT event loop is idle. Then you'd call QApplication::processEvents() or potentially QApplication::processEvents(QEventLoop::WaitForMoreEvents, 10) or similar to prevent your application from using too much CPU. 10 is the timeout value in ms, which you might want to tweak a bit to keep the application responsive. Alternatively you could use glutTimerFunc() to call QApplication::processEvents() only every so often.
I would avoid using threads if possible, since that is quite error-prone.