QOpenGLWidget gives incorrect h & w values.
-
In QOpenGLWidget are there any cases where the resize function doesn't has incorrect parameters for w and h? as u can c my QOpenglWidget has a resize params of 22 and 182, which are beyond obviously incorrect for a full screen application, the aspect ratio from snipping tool tells to be roughly 2.25
And its not the install it seems because I have another project which seems to give valid values in the resizeGL function.Nodes::Nodes(QWidget *parent) : QMainWindow(parent) { setupMenuBar(); setupStatusBar(); setupCentralWidget(); } void Nodes::setupCentralWidget() { centre = new QWidget(); QVBoxLayout* centralLayout = new QVBoxLayout(); tabBar = new QTabBar(); renderer = new Renderer(); centralLayout->addWidget(tabBar); centralLayout->addWidget(renderer); centre->setLayout(centralLayout); setCentralWidget(centre); //setCentralWidget(new Renderer()); }
here is the declaration of the widget which returns the invalid values
Melon::Melon(QWidget* parent) : QMainWindow(parent), propertyPanelDisplayed(false), window(new Preferences_Window(nullptr)) { setupStatusBar(); setupMenuBar(); setupToolBar(); setupworld(); setupDockWidget(); setupPropertyTab(); setupInputPanel(); displayPropertyTab(); } void Melon::setupworld() { setCentralWidget(new Renderer(this)); }
meanwhile this is the declaration of the widget which returns the size values correctly, Although im unsure predefination is the cause given the resize function is called at the app.show() function anyways so by then all the initialization regardless of order should be done, and I feel I have defined both the widgets correctly here
I have tried quite a bit to figure out what is wrong here, given I couldnt i am beginning to think maybe this is a bug which might need reporting but honestly im not sure. Thanks for the help in advacnce. -
Two things here: 2.25 is a common High DPI scaling factor on Windows. You have to configure your app appropriately to take the scaling into consideration or get the absolute unscaled device sizes. See here.
Second thing, the size of 22x182 suggest you are getting a minimum window size (considering the title bar decorations), which is what you get before the underlying native surface is created. Querying it directly aftershow()
is too soon. Native surface creation is deferred and any size query before the firstshowEvent()
is not valid and will give you that placeholder value. -
I mean I am not querying it explicitly, The resizeGL() function is never called explicitly and is always called by the Qt framework. Me saying that it is called after app.show() was because a friend of mine suggested that maybe this is because of improper initialization order on my end. That was just an argument to prove otherwise. Also It is true that the 182, 22 are placeholder values since the show and show maximized dont seem to make a difference but i couldnt understand what u meant by native surface creation, if u dont mind can u explain a bit in detail or link any resource for it? thanks
-
@SilentBatv-2 said in QOpenGLWidget gives incorrect h & w values.:
what u meant by native surface creation, if u dont mind can u explain a bit in detail or link any resource for it? thanks
The moment you call
show()
on a widget, it's about to show but not rendered properly (including all layouts and parent/child relations).
So the native surface where your widget is drawn is not fully initialized.To get any geometry after the widget is shown on the screen and respecting layouts etc. use the
QWidget
'sshowEvent()
. Call your function from there and you will get the values which match with what you actually see on your screen. -
i couldnt understand what u meant by native surface creation
Both Qt and OpenGL draw to OS provided surfaces e.g. a window identified by a window handle (HWND). Because of reasons I won't get into here your main window and QOpenGLWidget don't share that resource but create one for each and composite them over one other (well, not necessarily true, but lets keep it simple). Creating those underlying surfaces is relatively costly (in terms of memory consumption, system resources and speed), so when you create a widget it doesn't immediately allocate them. It is deferred to the moment a widget is actually made visible. Until that time a geometry of a widget is unspecified. You can explicitly call e.g.
setGeometry
orresize
, which do record a desired size, but that still doesn't allocate the native surface until the widget is made visible.I mean I am not querying it explicitly, The resizeGL() function is never called explicitly and is always called by the Qt framework
Both code snippets you've shown set up the widgets in the main window constructor, so they are shown at the same time and there is only one call to
resizeGL
from theshow()
call of MainWindow. That shouldn't exhibit the behavior you describe, so there must be something else you're doing and not showing here. In your callstack window right click anywhere and from the context menu tell it to show external code. It will show you what in yourmain()
is callingresizeGL
.