gpt4 book ai didi

c++ - 带有 QWidget 的 OSG 显示额外的边框

转载 作者:行者123 更新时间:2023-11-28 07:42:48 28 4
gpt4 key购买 nike

我修改了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

在指定大小策略时,我还尝试了 MinimumMinimumExpanding忽略,但效果相同。我试图直接在 ViewerWidget 中指定大小策略(因为它继承自 QWidget)并也指定它的 widget 属性,但没有成功。

最佳答案

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

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

布局通常在其元素周围插入填充(在 Qt 文档中称为边距)。您可以使用 QLayout::setContentsMargin() 对其进行调整,所以这里有一些本着此精神的东西

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

应该可以解决问题。

关于c++ - 带有 QWidget 的 OSG 显示额外的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15476265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com