OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

osgText::Text dissapears when reparent container window on qt

Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General
View previous topic :: View next topic  
Author Message

Joined: 17 Nov 2018
Posts: 24

PostPosted: Wed Mar 27, 2019 1:48 pm    Post subject:
osgText::Text dissapears when reparent container window on qt
Reply with quote


This is a mixed OSG/Qt question. I asked first here due to previous experiences with the community.

I'm following a minimal example of embedding OSG into a Qt5 application from When I modify that example, and add a simple osgText::Text to the base Geode everything works as expected. But If I ,additionally, encapsulate the OSG Widget (a widget derived from QOpenGLWidget) on a QDockWidget and undock it, the text dissapears.

Can someone point me out what could be the problem?

The code:



#include <iostream>
#include <stdio.h>

class QtOSGWidget : public QOpenGLWidget
  QtOSGWidget(qreal scaleX, qreal scaleY, QWidget* parent = 0)
      : QOpenGLWidget(parent)
        , _mGraphicsWindow(new osgViewer::GraphicsWindowEmbedded( this->x(), this->y(),
                                                                 this->width(), this->height() ) )
        , _mViewer(new osgViewer::Viewer)
      , m_scaleX(scaleX)
      , m_scaleY(scaleY)
        osg::Cylinder* cylinder    = new osg::Cylinder( osg::Vec3( 0.f, 0.f, 0.f ), 0.25f, 0.5f );
        osg::ShapeDrawable* sd = new osg::ShapeDrawable( cylinder );
        sd->setColor( osg::Vec4( 0.8f, 0.5f, 0.2f, 1.f ) );
        osg::Geode* geode = new osg::Geode;

      // adding text to the visualization
      osgText::Text * test = new osgText::Text();
      test->setColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));
      // end adding text

        osg::Camera* camera = new osg::Camera;
        camera->setViewport( 0, 0, this->width(), this->height() );
        camera->setClearColor( osg::Vec4( 0.9f, 0.9f, 1.f, 1.f ) );
        float aspectRatio = static_cast<float>( this->width()) / static_cast<float>( this->height() );
        camera->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f );
        camera->setGraphicsContext( _mGraphicsWindow );

        osgGA::TrackballManipulator* manipulator = new osgGA::TrackballManipulator;
        manipulator->setAllowThrow( false );
       // _mViewer->realize();

  virtual ~QtOSGWidget(){}

  void setScale(qreal X, qreal Y)
      m_scaleX = X;
      m_scaleY = Y;
      this->resizeGL(this->width(), this->height());


  virtual void paintGL() {

  virtual void resizeGL( int width, int height )
      this->getEventQueue()->windowResize(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
      _mGraphicsWindow->resized(this->x()*m_scaleX, this->y() * m_scaleY, width*m_scaleX, height*m_scaleY);
      osg::Camera* camera = _mViewer->getCamera();
      camera->setViewport(0, 0, this->width()*m_scaleX, this->height()* m_scaleY);

  virtual void initializeGL(){
      osg::Geode* geode = dynamic_cast<osg::Geode*>(_mViewer->getSceneData());
      osg::StateSet* stateSet = geode->getOrCreateStateSet();
      osg::Material* material = new osg::Material;
      material->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE );
      stateSet->setAttributeAndModes( material, osg::StateAttribute::ON );
      stateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );

  virtual void mouseMoveEvent(QMouseEvent* event)
      this->getEventQueue()->mouseMotion(event->x()*m_scaleX, event->y()*m_scaleY);

  virtual void mousePressEvent(QMouseEvent* event)
      unsigned int button = 0;
      switch (event->button()){
      case Qt::LeftButton:
          button = 1;
      case Qt::MiddleButton:
          button = 2;
      case Qt::RightButton:
          button = 3;
      this->getEventQueue()->mouseButtonPress(event->x()*m_scaleX, event->y()*m_scaleY, button);

  virtual void mouseReleaseEvent(QMouseEvent* event)
      unsigned int button = 0;
      switch (event->button()){
      case Qt::LeftButton:
          button = 1;
      case Qt::MiddleButton:
          button = 2;
      case Qt::RightButton:
          button = 3;
      this->getEventQueue()->mouseButtonRelease(event->x()*m_scaleX, event->y()*m_scaleY, button);

  virtual void wheelEvent(QWheelEvent* event)
      int delta = event->delta();
      osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ?
                  osgGA::GUIEventAdapter::SCROLL_UP : osgGA::GUIEventAdapter::SCROLL_DOWN;

  virtual bool event(QEvent* event)
      bool handled = QOpenGLWidget::event(event);
      return handled;


  osgGA::EventQueue* getEventQueue() const {
    osgGA::EventQueue* eventQueue = _mGraphicsWindow->getEventQueue();
    return eventQueue;

  osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _mGraphicsWindow;
  osg::ref_ptr<osgViewer::Viewer> _mViewer;
  qreal m_scaleX, m_scaleY;

int main(int argc, char** argv)
   // This was necessary for keeping the context when undocking

    QApplication qapp(argc, argv);

    QMainWindow window;

   // Here starts code to put the QtOSGWidget inside a dock area
   QDockWidget * visDock = new QDockWidget(&window);
   visDock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);

   QWidget * dockWidgetContent = new QWidget();

   QVBoxLayout * verticalLayout = new QVBoxLayout(dockWidgetContent);
   verticalLayout->setContentsMargins(11, 11, 11, 11);

   QWidget * containerWidget = new QWidget(dockWidgetContent);
   QHBoxLayout * horizontalLayout = new QHBoxLayout(containerWidget);
   horizontalLayout->setContentsMargins(11, 11, 11, 11);
   horizontalLayout->setContentsMargins(0, 0, 0, 0);
   QtOSGWidget* widget = new QtOSGWidget(1, 1, containerWidget);



   window.addDockWidget(static_cast<Qt::DockWidgetArea>(4), visDock);

   // No central widget is used for the example

    return qapp.exec();

Thank you!

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> General All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts How to handlle the window closing eve... blackball General 1 Wed May 01, 2019 9:10 am View latest post
No new posts How to add text in osgviewer application jaisingla General [forum] 0 Mon Apr 15, 2019 10:44 am View latest post
No new posts Colored Bounding Box over osgText::Text dhhabyc General 1 Fri Apr 05, 2019 12:43 pm View latest post
No new posts Cannot pick HUD text bclay General 3 Thu Feb 28, 2019 3:34 pm View latest post
No new posts osgprerender and render to Window Trajce Nikolov NICK General 0 Mon Feb 04, 2019 2:52 pm View latest post

Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP