2

I was just wondering if QOpenGLWidget that is on Qt (I am using version 5.4) can be used without subclassing.

In the Qt Creator, form editor, there is a Display widget that can be inserted into the form named "OpenGL Widget". The widget appears black when inserted and is QOpenGLWidget object. Compilation does not throw errors, such as the fact that the initializedGL() virtual function must be implemented.

The Qt help available and examples only show how to use this object by subclassing it. This kind of defeats the purpose of having an insertable object on the toolbar so I thought perhaps there could be a way.

I noticed that the widget has functions such as makeCurrent(), which I think could be useful.

Thanks

Luis P.
  • 21
  • 2

3 Answers3

1

This kind of defeats the purpose of having an insertable object on the toolbar so I thought perhaps there could be a way.

You have to subclass, but you also want the .ui file to use the widget of the correct derived class, not of the base class.

There are two ways to go about it:

  1. Insert the base class widget. Then promote it to the derived class. You can also do it manually by editing the .ui file.

  2. Make a custom widget Designer plugin for your class. Note that this plugin (and the widget!) have to be compiled using the same version/configuration of Qt used to compile Qt Creator itself, so in most cases you have to compile Qt Creator itself first.

If you're serious about development in Qt, you'll probably have your own Designer and Creator plugins, and you'll build Creator yourself, as well as Qt.

Community
  • 1
  • 1
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Thank you for your answer. But again I still don't understand why the Designer palette in QTCreator has an insertable QOpenGLWidget when in fact it has to be subclassed (or promoted) anyway. – Luis P. Aug 31 '16 at 22:58
  • Imagine it wasn't there. What would you promote from? `QWidget`. And you would lose access for all the properties that a `QOpenGLWidget` may have, that your derived widget also has. And also your `.ui` would be much less self-documenting. – Kuba hasn't forgotten Monica Sep 01 '16 at 13:09
0

You can do something like this by subclassing a shell widget

    void OpenGLDisplayWidget::initializeGL()
    {

        m_renderer->initialise(context());
    }

    void OpenGLDisplayWidget::resizeGL(int w, int h)
    {
        m_renderer->resize(w, h);
    }

    void OpenGLDisplayWidget::paintGL()
    {
        m_renderer->draw();
    }

So your rendering code isn't tied to the display widget.

Tom Kulaga
  • 138
  • 7
0

I just made a few experiments and I found that the QOpenGLWidget can be used very easily, without subclassing.

  1. Insert the "Open GL Widget" (QOpenGLWidget object)
  2. Insert a button and go to "clicked()" slot.
  3. Insert the following code

    void MainWindow::on_pushButton_clicked()
    {
        ui->openGLWidget->makeCurrent();
    
        //Paint a colorful triangle
    
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glBegin(GL_TRIANGLES);
        glColor3f(1.0, 0.0, 0.0);
        glVertex3f(-0.5, -0.5, 0);
        glColor3f(0.0, 1.0, 0.0);
        glVertex3f( 0.5, -0.5, 0);
        glColor3f(0.0, 0.0, 1.0);
        glVertex3f( 0.0,  0.5, 0);
        glEnd();
    }
    
  4. Run and click the button and you should see this screenshot

So no need to subclass, however it may be useful to include an initialization function that sets the projection matrix and aspect ratio.

Also these GL drawing functions must be called within the mainwindow which may not be desirable for the modular fanatics.

Luis P.
  • 21
  • 2