0

所以我有一个 Qt 应用程序。我在外面有一些基本的 Qt GUI,但是我有一个 Qt 小部件,OpenSceneGraph用于渲染 3D 场景。为了让屏幕内的事情变得更复杂,我有一个可以打开和关闭的 HUD。这个 HUD 由一些图形元素和一个渲染到纹理的 Qt Widget 组成。

我基本上可以正常工作,但是我在 HUD/Qt 小部件中遇到了一些尺寸问题。当我第一次将 HUD 切换为可见时,Qt 小部件在那里,但太大了。我可以更改大小,但无论如何,当我第一次按下一个键时,Qt 小部件会自动调整大小以适合我给它的纹理区域(这是我所期望的),但是这个自动调整大小的小部件不适合该区域正确。无法读取小部件包含的表格。

为了帮助我有两个屏幕截图。第一个是在我输入密钥之前:
http
://cerrnim.com/wp-content/uploads/2012/12/before.png 第二个是在我输入密钥之后:
http ://cerrnim.com/wp-内容/上传/2012/12/after.png

此外,这里有一些代码片段展示了我如何创建 HUD。它当然是更大计划的一部分,但希望这是足够的信息。

/* AUX function to create HUD geo. */                                           
osg::Geode* HUDGeometry( int x1, int y1, int x2, int y2,                        
    std::string model, HUDEvents event, NetworkViewer* viewer ) {               
  osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(x1,y1,0),     
                          osg::Vec3(x2-x1,0,0), osg::Vec3(0,y2-y1,0), 1, 1);    
  osg::Geode* geode = new osg::Geode( ) ;                                       

  geode->setName( model ) ;                                                     
  geode->setUserData( new HUDEvent( event, viewer ) ) ;                         

  osg::Texture2D* HUDTexture = new osg::Texture2D();                            
  HUDTexture->setDataVariance(osg::Object::DYNAMIC);                            
  osg::Image* hudImage = osgDB::readImageFile(model);                           
  HUDTexture->setImage(hudImage);                                               
  geode->getOrCreateStateSet()->setTextureAttributeAndModes(                    
    0, HUDTexture, osg::StateAttribute::ON);                                    

  geode->addDrawable( quad ) ;                                                  

  return geode ;                                                                
}      

/* Creates the HUD but does not display it yet. */                              
void NetworkViewer::initHUD( ) {                                                
  osg::MatrixTransform* mt = new osg::MatrixTransform;                          

  osg::Camera* hudCamera = new osg::Camera;                                     
  hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);                    
  hudCamera->setViewMatrix(osg::Matrix::identity());                            
  //hudCamera->setProjectionResizePolicy(osg::Camera::FIXED);                   
  hudCamera->setProjectionMatrixAsOrtho2D(0,100,0,100);                         
  hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);                                 
  hudCamera->setRenderOrder(osg::Camera::POST_RENDER);                          
  hudCamera->setAllowEventFocus(true);                                          

  QWidget* widget = new QWidget;                                                
  layout = new QVBoxLayout( ) ;                                                 
  widget->setLayout(layout);                                                    
  widget->layout()->addWidget(((GUI*)view)->getTabs( ));                        
  //widget->setGeometry(0, 0, 500, 400);                                        

  osg::ref_ptr<osgQt::QWidgetImage> widgetImage = new osgQt::QWidgetImage(widget);

  osg::Geometry* quad = osg::createTexturedQuadGeometry(osg::Vec3(30,32,0),     
                                  osg::Vec3(40,0,0), osg::Vec3(0,35,0), 1, 1);  
  osg::Geode* geode = new osg::Geode;                                           
  geode->addDrawable(quad);                                                     

  osg::Texture2D* texture = new osg::Texture2D(widgetImage.get());              
  texture->setResizeNonPowerOfTwoHint(false);                                   
  texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);            
  texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);          
  texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);          
  mt->getOrCreateStateSet()->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);

  mt->addChild(hudCamera);                                                      
  osgViewer::InteractiveImageHandler* handler =                                 
    new osgViewer::InteractiveImageHandler(widgetImage.get(), texture, hudCamera);

  mt->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);    
  mt->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);        
  mt->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);  
  mt->getOrCreateStateSet()->setAttribute(new osg::Program);                    

  quad->setEventCallback(handler);                                              
  quad->setCullCallback(handler);                                               

  hudCamera->addChild( geode ) ;                                                
  hudCamera->addChild( HUDGeometry(73,73,75,75,                                 
    "models/quit.png", EXIT_OBJ, this ));                                       
  hudCamera->addChild( HUDGeometry(25,25,75,75,                                 
    "models/hud.png", NO_EVENT, this ));           

  osg::Group* overlay = new osg::Group;                                         
  overlay->addChild(mt);                                                        

  root->addChild(overlay);                                                      
  HUD = hudCamera ;                                                             
  disableHUD( ) ;                                                               
}
4

1 回答 1

0

我认为主要问题是您没有使小部件的尺寸适应您渲染它的四边形的尺寸。

我不确定QWidgetImage内部在做什么,但我想它只是将小部件渲染到适当大小的画布上并将结果转换为图像。在您的代码中,您将完整的图像(无论其尺寸或纵横比)映射到该四边形。如果您希望小部件适合四边形,则需要在创建它的图像之前调整小部件的大小。

于 2012-12-12T22:11:51.950 回答