Skip to content

override sizeHint in osgQOpenGLWidget #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2020
Merged

override sizeHint in osgQOpenGLWidget #42

merged 1 commit into from
Aug 23, 2020

Conversation

gramnation
Copy link

@gramnation gramnation commented Aug 23, 2020

Default value of QWidget::rect is 0,0,640,480. This SD aspect ratio agrees with OSG when widget is shown by itself. However, once widget is put into a layout it has to return a valid sizeHint in order to preserve this aspect ratio and size.
https://doc.qt.io/qt-5/qwidget.html#size-hints-and-size-policies

"When implementing a new widget, it is almost always useful to reimplement sizeHint() to provide a reasonable default size for the widget and to set the correct size policy with setSizePolicy()."   
https://doc.qt.io/qt-5/qwidget.html#size-hints-and-size-policies  

Default value of QWidget::rect is 0,0,640,480. This SD aspect ratio agrees with OSG when widget is shown by itself. However, once widget is put into a layout it has to return a valid sizeHint in order to preserve this aspect ratio and size.
@gramnation gramnation changed the title Update osgQOpenGLWidget override sizeHint in osgQOpenGLWidget Aug 23, 2020
@mathieu mathieu merged commit 2cb7067 into openscenegraph:master Aug 23, 2020
@mathieu
Copy link
Collaborator

mathieu commented Aug 23, 2020

thanks for the PR @gramnation !

@gramnation gramnation deleted the patch-1 branch August 23, 2020 17:51
@forsnowlove
Copy link

I did like this and it worked! Thank you !
setp one:override sizeHint
class MosgQOpenGLWidget : public osgQOpenGLWidget
{
public:
MosgQOpenGLWidget();
QSize sizeHint() const override;
private:
};
QSize MosgQOpenGLWidget::sizeHint() const
{
//return QSize(640,480);
//qDebug()<<"hint"<width()<<" "<height();
return QSize(this->width(),this->height());
}
setp two:camera->setProjectionMatrixAsPerspective()
pOsgW = new MosgQOpenGLWidget;
pViewer = pOsgW->getOsgViewer();
QSize size = pOsgW->sizeHint();
float aspectRatio = static_cast( size.width()) / static_cast( size.height() );
pViewer->getCamera()->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f );

@shaozhihua
Copy link

if only this function is a pure virtual function,then we will know the usage..
anyway.thank you @gramnation

@HorrZzz
Copy link

HorrZzz commented Mar 8, 2024

我确实喜欢这个,它奏效了!谢谢! setp one:override sizeHint 类 MosgQOpenGLWidget : public osgQOpenGLWidget { public: MosgQOpenGLWidget();QSsize sizeHint() const 覆盖;私人: }; QSize MosgQOpenGLWidget::sizeHint() const { //return QSize(640,480); //qDebug()<<“hint”<width()<<“ ”<height();返回QSize(this->width(),this->height()); } setp two:camera->setProjectionMatrixAsPerspective() pOsgW = new MosgQOpenGLWidget; pViewer = pOsgW->getOsgViewer(); QSize 大小 = pOsgW->sizeHint(); float aspectRatio = static_cast( size.width()) / static_cast( size.height() ); pViewer->getCamera()->setProjectionMatrixAsPerspective( 30.f, aspectRatio, 1.f, 1000.f );

It's effective. Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants