[osg-users] osgQt & osgEarth not working with 0 margin in Qlayout

David Bobavid david.bobavid at gmail.com
Thu May 16 07:16:05 PDT 2019


Hi,

I've come across an issue I'm sort of confused about, and hopefully someone can help figure it out. 

I'm using osgQt & osgEarth 2.10 & osg 3.6.3 with Qt 5.12 on Windows 10. It all seems to work, and I'm following the osgQt Viewer example for setting up my program. My osg widget, when added to my frame, has the default margin and spacing, so when I set it to my central widget (in a QMainWidget) it has a gray border. However, when I set the margins to 0, it won't draw the map at all, and it shows some artifacts around the edge of the widget. 

This is what I'm doing:

Code:

OsgViewerQWidget::OsgViewerQWidget(QWidget* parent /*= 0*/, Qt::WindowFlags f /*= 0*/, QStatusBar* statusBar /*= 0*/)
: QWidget(parent, f)
{
 _map = new osgEarth::Map();

// add a TMS imagery layer: 
osgEarth::Drivers::TMSOptions imagery;
imagery.url() = "http://readymap.org/readymap/tiles/1.0.0/7/";
_map->addLayer(new osgEarth::ImageLayer("ReadyMap Imagery", imagery));

// add a TMS elevation layer:
osgEarth::Drivers::TMSOptions elevation;
elevation.url() = "http://readymap.org/readymap/tiles/1.0.0/116/";
_map->addLayer(new osgEarth::ElevationLayer("ReadyMap Elevation", elevation));

graticuleLayer = new osgEarth::Util::GeodeticGraticule();
_map->addLayer(graticuleLayer);

// That's it, the map is ready; now create a MapNode to render the Map:
_mapNode = new osgEarth::MapNode(_map);

_mapNode->setNodeMask(0x1);

setThreadingModel(osgViewer::ViewerBase::SingleThreaded);

// disable the default setting of viewer.done() by pressing Escape.
setKeyEventSetsDone(0);

QWidget* widget1 = AddViewWidget(CreateGraphicsWindow(0, 0, 100, 100), _mapNode, statusBar);

QGridLayout* grid = new QGridLayout;
setLayout(grid);

grid->setSpacing(0);
//grid->setContentsMargins(0, 0, 0, 0);   ////// with this line commented out, it works, with it in, it doesn't work... but, why?!
grid->addWidget(widget1);
	
connect(&_timer, SIGNAL(timeout()), this, SLOT(update()));
_timer.start(10);
}

osgQt::GraphicsWindowQt* OsgViewerQWidget::CreateGraphicsWindow(int x, int y, int w, int h, const std::string& name /*= ""*/, bool windowDecoration /*= false*/)
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();

return new osgQt::GraphicsWindowQt(traits.get());
}

QWidget* OsgViewerQWidget::AddViewWidget(osgQt::GraphicsWindowQt* gw, osg::ref_ptr<osg::Node> scene, QStatusBar *statusBar)
{
osgViewer::View* view = new osgViewer::View;
addView(view);

osg::Camera* camera = view->getCamera();
camera->setGraphicsContext(gw);

// need to do this to make lines show up... o_O  Thanks's Glenn!
osgEarth::Util::GLUtils::setGlobalDefaults(camera->getOrCreateStateSet());

const osg::GraphicsContext::Traits* traits = gw->getTraits();

camera->setClearColor(osg::Vec4(0.2, 0.2, 0.6, 1.0));
camera->setViewport(new osg::Viewport(0, 0, traits->width, traits->height));

// set the draw and read buffers up for a double buffered window with rendering going to back buffer
camera->setDrawBuffer(GL_BACK);
camera->setReadBuffer(GL_BACK);

camera->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(traits->width) / static_cast<double>(traits->height), 1.0f, 10000.0f);

view->setSceneData(scene);
view->addEventHandler(new osgViewer::StatsHandler);
view->addEventHandler(new osgGA::StateSetManipulator(view->getCamera()->getOrCreateStateSet()));
view->addEventHandler(new osgViewer::ThreadingHandler());
view->addEventHandler(new osgViewer::LODScaleHandler());
view->addEventHandler(new osgViewer::ScreenCaptureHandler());
	
earthManip = new osgEarth::Util::EarthManipulator;
view->setCameraManipulator(earthManip);

ldBuffer = new osgEarth::Util::LogarithmicDepthBuffer;
ldBuffer->install(camera);

gw->setTouchEventsEnabled(true);
return gw->getGLWidget();
}




So it seems to go wrong in my constructor, when I set the grid layout content margins to 0. Otherwise this works just fine. I set the margin to 0, and it doesn't show anything. 

Any ideas will be greatly appreciated.

Thank you!
David

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=76113#76113







More information about the osg-users mailing list