osgEarth在带有0边距的Qt小部件中不可见

时间:2019-05-28 17:04:30

标签: qt openscenegraph osgearth

在将小部件的边距设置为0时,尝试在Qt窗口中显示osgEarth小部件时遇到麻烦。

我在Windows 10上使用带有Qt 5.12的osgQt&osgEarth 2.10&osg 3.6.3。这一切似乎都有效,并且我遵循osgQt Viewer示例来设置程序。我的osg小部件添加到框架后,具有默认的边距和间距,因此当我将其设置为中央小部件(在QMainWidget中)时,它具有灰色边框。但是,当我将页边距设置为0时,它根本不会绘制地图,并且在小部件边缘周围显示了一些伪像。

这就是我在做什么:

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(); 
} 

这正在加载到QMainWidget中,在这里我将创建OsgViewingQWidget的实例:

viewWidget = new OsgViewerQWidget(0, Qt::Widget, ui.statusBar);
viewWidget->SetViewpoint(21.306622, -157.934527, 0);

setCentralWidget(viewWidget);

无论我将父级设置为0还是这个,它似乎都没有改变。

设置边距后,它将显示蓝色背景(就像它试图加载基础层之类的东西一样,在窗口的左下角和顶部,我会看到一些灰线地图或其他任何内容都不会显示,但我也看不到任何控制台错误。

任何帮助将不胜感激! 大卫

0 个答案:

没有答案