带有QWidget的OSG显示了额外的边框

时间:2013-03-18 11:56:51

标签: c++ qt openscenegraph

我修改了osgViewerQt example以加载点云并在Qt应用程序中将其可视化。正如您在附图中看到的那样,显示了云点云,但窗口中有一个额外的边框(参见箭头)。

Extra borders in window

我花了整个周末试图计算如何“扩展”窗口以移除边框,但它一直在显示。

你知道我该怎么做才能删除它?我将发布修改后的osgViewerQt的代码和我使用它的代码片段。

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

现在,在使用此小部件的地方(简化了一点以仅显示相关部分):

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

在指定尺寸政策时,我还尝试使用MinimumMinimumExpandingIgnored,但效果相同。我试图在ViewerWidget内直接指定大小策略(因为它继承自QWidget)并将其指定为widget属性,但没有成功。

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,您正在使用网格布局在目标窗口中插入视图窗口小部件:

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

布局通常在元素周围插入填充(在Qt文档中称为边距)。你可以使用QLayout::setContentsMargin()来调整它,所以这里有一些本着

的精神
grid->setContentsMargins(0,0,0,0);

应该这样做。