I've been trying to learn Qt for the purposes of embedding a GUI on top of my OpenGL projects. In this case, the idea is to have my OpenGL viewport fill my main window. I have a simple QtWidget-based GUI that contains a 'RenderSurface' which is a subclass of QGLWidget. However, I've noticed that making my RenderSurface a child of my MainWindow has unwanted effects on my GUI's layout.
Here's the simple MainWindow code and screen shot without use of parenting:
#include "mainwindow.h"
#include "rendersurface.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
renderSurface()
{
setCentralWidget(&renderSurface);
setWindowTitle("QtGL Test");
}
MainWindow::~MainWindow()
{
}
In this case, my RenderSurface (QGLWidget subclass) is not passed a pointer to any parent QWidget when I call its constructor in the MainWindow initialization list. You can see that the dark grey OpenGL context fits the size of the window, and it seems to fill the window even when I expand and contract the window size.
Here's the same thing with parenting:
#include "mainwindow.h"
#include "rendersurface.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
renderSurface(this)
{
setCentralWidget(&renderSurface);
setWindowTitle("QtGL Test");
}
MainWindow::~MainWindow()
{
}
Simply passing 'this' to the RenderSurface constructor instead changes the way that my OpenGL context is initially rendered in my window. It's also worth noting that my RenderSurface will start filling my window properly once I resize the window by dragging an edge.
Why does making my RenderSurface a child of my MainWindow cause this issue? How can I avoid this buggy-looking behaviour? Also, since my GUI seems to work better without parenting, what are some pros and cons of object parenting in Qt?