Question

I have modified the osgViewerQt example in order to load a point cloud and visualize it in a Qt application. As you can see in the attached image, the cloud point cloud is shown but there is an extra border in the window (see the arrows).

Extra borders in window

I spent all the weekend trying to figure how to "expand" the window in order to remove that border, but it keeps showing.

Do you know what can I do to remove it? I'll post the code for the modified osgViewerQt and the piece of code where I use it.

viewer_widget.h

#ifndef VIEWER_WIDGET_H
#define VIEWER_WIDGET_H

#include "osgViewer/CompositeViewer"

#include <QTimer>
#include <QWidget>

class QGridLayout;
class QWidget;

class ViewerWidget : public QWidget, public osgViewer::CompositeViewer {
private:
  std::string cloud_file;
  std::string cloud_filepath;
  QTimer timer_;
  QWidget* widget;
  QGridLayout* grid;
  osg::ref_ptr<osgViewer::View> view;
private:
  ViewerWidget(const ViewerWidget& V);
  ViewerWidget& operator=(const ViewerWidget& V);
private:
  QWidget* AddViewWidget(osg::Camera* camera,osg::Node* scene);
  osg::Camera* CreateCamera(int x,int y,int w,int h,const std::string& name="",
    bool windowDecoration=false
  );
  osg::Node* ReadOctree(const std::string& file);
public:
  ViewerWidget(const std::string& filename,const std::string& filepath,bool color,
    osgViewer::ViewerBase::ThreadingModel threadingModel
      = osgViewer::CompositeViewer::ThreadPerCamera
  );
  virtual ~ViewerWidget(void){}
  void AddCloud(void);
  void StartFrameTimer(int msec=10) { timer_.start(msec); }
  virtual void paintEvent( QPaintEvent* event ) { frame(); }
};

#endif // VIEWER_WIDGET_H

osg_viewer.cpp

#include "viewer_widget.h"

#include "osgDB/ReadFile"
#include "osgGA/TrackballManipulator"
#include "osgQt/GraphicsWindowQt"
#include "osgViewer/ViewerEventHandlers"

#include <QGridLayout>
#include <QDebug>

ViewerWidget::ViewerWidget(const std::string &filename,const std::string &filepath,
  bool color, osgViewer::ViewerBase::ThreadingModel threadingModel
) :
  QWidget(),
  cloud_file( filename ),
  cloud_filepath( filepath )
{
//  this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
  connect( &(this->timer_), SIGNAL(timeout()), this, SLOT(update()) );
}

QWidget* ViewerWidget::AddViewWidget(osg::Camera *camera,osg::Node *scene) {
  view = new osgViewer::View;
  view->setCamera( camera );
  view->setSceneData( scene );

  osg::Stats* stats = this->getViewerStats();
  if(stats) stats->report(std::cout);

  addView( view );
  view->addEventHandler( new osgViewer::StatsHandler );
  view->setCameraManipulator( new osgGA::TrackballManipulator );
  osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>(
    camera->getGraphicsContext()
  );

  return gw ? gw->getGLWidget() : 0;
}

osg::Camera* ViewerWidget::CreateCamera(int x,int y,int w,int h,const std::string &name,
  bool windowDecoration
) {
  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;
  qDebug() << "w:" << w << " h:" << h;
  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();
  osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
  camera->setClearColor( osg::Vec4(0,0,0,1) );
  camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
  camera->setProjectionMatrixAsPerspective(
      30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );

  return camera.release();
}

osg::Node* ViewerWidget::ReadOctree(const std::string &file) {
  osg::Group* group = new osg::Group;
  group->addChild( osgDB::readNodeFile(file, options) );
  return group;
}

void ViewerWidget::AddCloud() {
  std::cout << "Loading cloud from file:" << cloud_file.c_str() << "\n";
  QWidget* widget = AddViewWidget(
    CreateCamera(0,0,100,100,"cam1",true),
    ReadOctree(cloud_file)
  );
  grid = new QGridLayout;
  grid->addWidget( widget, 0, 0 );
  this->setLayout( grid );
}

Now, in where this widget is used (simplified a bit to show only the relevant parts):

cloud.h

#ifndef CLOUD_H
  class Cloud: public QObject {
  Q_OBJECT
  private:
    osg::ref_ptr<ViewerWidget> osg_widget;
    QDockWidget* dock;
    /// MORE ATTRIBUTES
  public:
    Cloud(){
      /// ...
      dock = new QDockWidget;
      osg_widget = new ViewerWidget( getFileName(), getFilePath(), has_color);
      dockWidget->setAllowedAreas(Qt::RightDockWidgetArea);
      dockWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
      dockWidget->addWidget(osg_widget);
    }
    /// More methods, cloud manipulators, etc.
  };
#endif

When specifying the size policy, I have also tried with Minimum, MinimumExpanding and Ignored, but with the same effect. I tried to specify the size policy directly inside the ViewerWidget (as it inherits from QWidget) and to specify it its widget attribute, too, but with no success.

Était-ce utile?

La solution

You're using a grid layout to insert the view widget in your target window, if I understand correctly:

grid = new QGridLayout;
grid->addWidget( widget, 0, 0 );
this->setLayout( grid );

Layouts usually insert padding around their elements (called margin in the Qt docs). You can tune that using QLayout::setContentsMargin(), so here something in the spirit of this

grid->setContentsMargins(0,0,0,0);

should do the trick.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top