ShimmerFairy
ShimmerFairy

Reputation: 81

How do I actually use OpenGL in Qt 5?

Simply put, I cannot for the life of me figure out how to actually make use of things like QOpenGLWidget or QOpenGLWindow or anything. I want to have the rendering I do be a child widget of a window in a MDI, but nothing works.

Here's the code I have currently set up for the widget (at least, just the parts involving OpenGL):

Viewport::Viewport(QWidget * parent) : QOpenGLWidget(parent) { }

void Viewport::initializeGL() {
    initializeOpenGLFunctions();
}

void Viewport::paintGL() {
    // first, clear the screen
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
}

And here's how I use the widget:

vp = new Viewport;
vp->resize(QSize(320,240));
hbox->addWidget(vp);

And the result is that I see nothing. I just see a tiny sliver of empty space next to the other widget in the window, but that's it. No black screen like I try to clear with, not even a 320x240-sized empty space.

Like I said, I've been unable to do this in any of the ways I could find, and it's really frustrating. Am I missing something obvious? There's very little documentation as-is, so it's hard to tell if I am, or if there's some weird corner case I'm running into. (For example, none of the documentation I find uses QOpenGLWidget as part of a larger widget; is that because it can't be, or because all the examples I can find are just lazy about using the widget as its own top-level window?)

Upvotes: 1

Views: 758

Answers (1)

ShimmerFairy
ShimmerFairy

Reputation: 81

After some more fiddling around, it turns out my issue was apparently caused by the other object in the window (a QListView) by default taking up as much space as possible, making the OpenGL widget disappear since it doesn't have a minimum size.

In other words, the problem is fixed by either changing the QListView to have a QSizePolicy::Preferred sizing policy (since resizing the window will now let you see the OpenGL widget), or by giving the OpenGL widget a minimum or fixed size.

(As an aside, I really wish this could've been more obvious than just stumbling upon it by chance.)

Upvotes: 1

Related Questions