opengl - Integrate Qt into prewritten Application/Framwork -
i have small visualization framework written in c++ , want use qt have proper gui , control on visualizations interact them. using glut create window , draw view in it. initialize object of class visualization me: holding model , views. view holds controller process user input , manipulate model. main loop looks this:
visualization* vis = new visualization(); vis->createmodel("some_file.txt"); vis->createview("unknown"); // ... void demo_app::display() { // clear framebuffer glclearcolor(0.3, 0.3, 0.3, 1.0); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); vis.update(); // looks dirty scenes update vis.render(); // draws views stored in vis glutswapbuffers(); }
with glut have single window in arrange multiple views. when order view draw during main loop can execute glcommands , fine.
so problem want use qt several windows (qdialogs
+qglwidgets
). familiar qt , ported framework qt. problem have qt integration within framework , not want. want control visualization qt , gui elements, draw call view should made visualization class shown glut example. there must way give inherited qglwidget
pointer view object , whenever view should drawn widget has call makecurrent()
glcommands can draw on context. can use texture in view paints current scene , draw texture in paintgl or gldraw()
function of qglwidget
, how can tell widget update()
when view has no knowledge it. emulate main loop qtimer
set 0. want able trigger rendering of qglwidget
inside framework. suggestion? links? examples?
so there must way give inherited qglwidget pointer view object , whenever view should drawn
if call demo_app::display()
within qglwidget::paintgl
, work , draw scene onto qglwidget
context. however, use qglwidget
's context, you'll need move initialization functions qglwidget
. don't remember whether opengl objects can shared across context, won't surprised if can't done.
but how can tell widget update() when view has no knowledge it.
well, add pointer qwidget
view , call qwidget->update()
. should obvious.
if want draw qt widgets within glut-based window...
i.e. if want "embed" qt widgets glut window.
every qt widget has render
method. can use method draw widget onto qpaintdevice
or using qpainter
.
so, draw widget within framework, you'll have provide 1 of widget , call render manually.
you'll have forward mouse , keyboard event framework , convert them qt events.
that should doable major pain implement.
also see embeddeddialogs demo , qgraphicsproxywidget.
if want use gui widgets in addition glut window, without embedding them glut window...
wrap related framework , glut messages qobject
, , put glut message handling object's slot. connect slot timer running 0
timeout.
example code (uses sdl):
class game: public qobject{ q_object protected: void processsdlevents(); ... protected slots: void ongameend(); void timerslot(); ... }; game::game(/*arguments*/ ...){ ... gametimer = new qtimer(this); gametimer->setsingleshot(false); gametimer->setinterval(0); connect(gametimer, signal(timeout()), this, slot(timerslot())); ... } void game::timerslot(){ processsdlevents(); update(); render(); } void game::processsdlevents(){ sdl_event event; qlist<gameeventhandler*> eventhandlers = findchildren<gameeventhandler*>(); /*warning: not infinite loop. */ while (sdl_pollevent(&event) != 0){ if (event.type == sdl_quit){ emit gameended(); continue; } bool processed = false; (int = 0; < eventhandlers.size(); i++){ if (eventhandlers[i]->processevent(&event)){ processed = true; break; } } if (processed) continue; (int = 0; < joysticks.size(); i++){ if (joysticks[i]->processevent(&event)){ processed = true; break; } } /*if (processed) continue;*/ } } int main(int argc, char** argv){ qapplication app(argc, argv); int result = 0; try{ game game; qobject::connect(&game, signal(gameended()), &app, slot(quit())); game.start(); result = app.exec(); } catch(const qstring& s){ reportexception(s); } catch(std::exception& e){ reportexception(e); } catch(...){ reportexception(); } return result; }
please note processsdlevents not infinite loops. gets called once in while , processes events received since last call , terminates.
in scenario framework specific functions called within qt main loop.
you other way around , call qt-specific functions main loop in own framework (using either qeventloop
class or qapplication::processevents()
) might less reliable , haven't tried myself.
Comments
Post a Comment