Hi Zoe,
viewer.run() runs an frame loop till the viewer has it's done flag set, very
much in the same way that QApplication::exec() runs it's event loop till
exit. It's not that Qt and OSG aren't compatible, it's just this two very
high level convenience functions both assume control of the main thread.
You have lots of options available to you, which is most appropriate depends
a great deal on what you want to do with your app. One route would be to
run the OSG's main thread in it own thread, another route would be to write
the frame loop and then prod the Qt events queue on each new frame, another
would be to use a Qt timer to call the Viewer::frame().
The best real-time solution would be to use a separate thread to run the
viewer's frame loop. I've attached part of example that hasn't yet made it
into the core OpenSceneGraph distribution, but uses either the frame loop
thread or interleaving of the Qt and OSG in one main thread. The key bits
of interest are:
if (useFrameLoopThread)
{
// create a thread to run the viewer's frame loop
ViewerFrameThread viewerThread(viewer.get(), true);
viewerThread.startThread();
// no start the standard Qt event loop, then exists when the
viewerThead sends the QApplication::exit() signal.
return QApplication::exec();
}
else
{
// run the frame loop, interleaving Qt and the main OSG frame loop
while(!viewer->done())
{
// process Qt events - this handles both events and paints the
browser image
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
viewer->frame();
}
return 0;
}
Hope this helps,
Robert.
On Tue, Sep 15, 2009 at 3:47 PM, Zoe Catz <[email protected]> wrote:
>
> Philip Lowman wrote:
> > Can you post a small example of the problem?
> The problem seems to be a threading problem. I'm new to osg so probably
> the solution is pretty easy?!
>
> Here's my sample programm which opens 1 qt and 1 osg window, but only one
> is active at the same time (in this case the osg window until I close it,
> then the qt window is active incl. my protocol in the background):
>
>
> Code:
> #include "Settings.h"
> #include <QtGui/QApplication>
> //#include "Scene.h"
> #include <osg/ShapeDrawable>
> #include <osg/Node>
> #include <osg/Group>
> #include <osgViewer/Viewer>
> #include <osgGA/TrackballManipulator>
>
> int main(int argc, char *argv[])
> {
> QApplication app(argc, argv);
> Settings settings;
> settings.show();
>
> //Scene* scene = new Scene();
> //scene->init();
> //test scene
> osgViewer::Viewer viewer;
> osg::ref_ptr<osg::Group> root (new osg::Group);
> osg::ref_ptr<osg::Geode> boxGeode (new osg::Geode);
> osg::ref_ptr<osg::Box> box (new osg::Box(osg::Vec3f(2,0,0), 0.4, 0.4,
> 0.4));
> osg::ref_ptr<osg::ShapeDrawable> boxDrawable (new
> osg::ShapeDrawable(box.get()));
> boxGeode->addDrawable(boxDrawable.get());
> root->addChild(boxGeode.get());
> viewer.setSceneData( root.get());
> viewer.setCameraManipulator(new osgGA::TrackballManipulator());
> viewer.setUpViewInWindow(150,150,800,600);
> viewer.run();
>
> return app.exec();
> }
>
>
>
>
> Philip Lowman wrote:
> > Have you considered using something like a QTimer to send your keep-alive
> messages instead?
>
> I did, but I really want to avoid to spend time on understanding the dmx
> artnet protocoll and it's polling policy, especially as it works completely
> automatically in other applications.
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=17385#17385
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> [email protected]
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
/* OpenSceneGraph example, osgcompositeviewer.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <iostream>
#include <osg/Notify>
#include <osg/io_utils>
#include <osg/ArgumentParser>
#include <osgDB/WriteFile>
#include <osgGA/TrackballManipulator>
#include <osgGA/StateSetManipulator>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgWidget/Browser>
#include <QtWebKit/QWebSettings>
#include <QtWebKit/QtWebKit>
#include <QtGui/QGraphicsScene>
#include <QtGui/QGraphicsView>
#include <QtGui/QApplication>
#include <QtGui/QPainter>
#include <QtGui/QtEvents>
#include "QGraphicsViewAdapter.h"
#include "QWebViewImage.h"
// Thread that runs the viewer's frame loop as we can't run Qt in the background...
class ViewerFrameThread : public OpenThreads::Thread
{
public:
ViewerFrameThread(osgViewer::ViewerBase* viewerBase, bool doQApplicationExit):
_viewerBase(viewerBase),
_doQApplicationExit(doQApplicationExit) {}
~ViewerFrameThread()
{
cancel();
while(isRunning())
{
OpenThreads::Thread::YieldCurrentThread();
}
}
int cancel()
{
_viewerBase->setDone(true);
return 0;
}
void run()
{
int result = _viewerBase->run();
if (_doQApplicationExit) QApplication::exit(result);
}
bool _doQApplicationExit;
osg::ref_ptr<osgViewer::ViewerBase> _viewerBase;
};
int main(int argc, char **argv)
{
// Qt requires that we construct the global QApplication before creating any widgets.
QApplication app(argc, argv);
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
bool useFrameLoopThread = true;
if (arguments.read("--no-frame-thread")) useFrameLoopThread = false;
osg::ref_ptr<QWebViewImage> image = new QWebViewImage;
if (arguments.argc()>1) image->navigateTo((arguments[1]));
else image->navigateTo("http://www.youtube.com/");
osgWidget::GeometryHints hints(osg::Vec3(0.0f,0.0f,0.0f),
osg::Vec3(1.0f,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,1.0f),
osg::Vec4(1.0f,1.0f,1.0f,1.0f),
osgWidget::GeometryHints::RESIZE_HEIGHT_TO_MAINTAINCE_ASPECT_RATIO);
osg::ref_ptr<osgWidget::Browser> browser = new osgWidget::Browser;
browser->assign(image.get(), hints);
// image->focusBrowser(true);
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer(arguments);
viewer->setSceneData(browser.get());
viewer->setCameraManipulator(new osgGA::TrackballManipulator());
viewer->addEventHandler(new osgViewer::StatsHandler);
viewer->addEventHandler(new osgViewer::WindowSizeHandler);
if (useFrameLoopThread)
{
// create a thread to run the viewer's frame loop
ViewerFrameThread viewerThread(viewer.get(), true);
viewerThread.startThread();
// no start the standard Qt event loop, then exists when the viewerThead sends the QApplication::exit() signal.
return QApplication::exec();
}
else
{
// run the frame loop, interleaving Qt and the main OSG frame loop
while(!viewer->done())
{
// process Qt events - this handles both events and paints the browser image
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
viewer->frame();
}
return 0;
}
}
_______________________________________________
osg-users mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org