Why won't my qt centralWidget (a QGLWidget) resize properly?
-
28-06-2021 - |
Domanda
I have an application with a QMainWindow that should ideally have a QGLWidget centered on it, with spaces left around the outside for toolbars and other top/side widgets.
Unfortunately, no matter what size or geometry the QGLWidget is set to, it appears enormous and takes up the entire main window!
Basically, the setup is as follows: CreateWindow() is called in main(), which constructs the main window and calls a function Init(), which constructs the GLWidget. So, within Init(), which is a member of my adapted main window class, I essentially have:
GLScene = new MyQGLWidget(this);
setCentralWidget(GLScene);
Now, to make the GLScene conform to its desired size and position, I first tried setting its geometry within the GLWidget (a.k.a. my class derived directly from GLWidget) constructor:
MyQGLWidget::MyQGLWidget(QWidget* parent){
....
setGeometry(210, 40, 600, 400); //the main window is of course bigger and should fit this nicely
....
}
This, however, didn't work at all and still made it take up the entire main window. Instead, I tried putting the setGeometry call into Init(). I tried it both right before and right after setCentralWidget(); neither option worked. Nor did flat-out removing the setCentralWidget call (in fact, this made the GLWidget disappear).
Since this clearly isn't working properly, what is the correct way to scale the GLWidget? Or is it just a matter of how things are ordered that I need to look into more deeply?
Soluzione
You need to add a layout to it. I have exactly the same thing ( a widget derived from QGLWidget) whose parent calls the resize function. This solved all my issues and made it to where I can change its size at runtime.