Is there an OpenGL Widget constructor for QT5?

I am porting some code from FLTK to QT5 and I can't get a graphic design widget that matches the OpenGL context? Does such a widget exist?

I built QT from official sources, targeting VS2012x64 to OpenGL and tried adding QT += opengl

to project.pro file.

+3


source to share


1 answer


Qt has a QGLWidget, but you shouldn't use it directly in the constructor. Instead, you should place the layout where you want the OpenGL widget to appear. Then you subclass the QGLWidget since you still have to overwrite the function paintGL

in order to draw something. Then, after the call, setupUI()

you will create your own GL widget and add it to the layout you placed in the designer withlayoutinstance->addWidget(…)

update due to comment

mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>335</width>
    <height>191</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout_2">
    <item>
     <layout class="QVBoxLayout" name="verticalLayout"/>
    </item>
   </layout>
  </widget>
  <action name="actionQuit">
   <property name="text">
    <string>&amp;Quit</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>

      

myglwidget.hh



#include <QGLWidget>

class MyGLWidget : public QGLWidget
{ //...
};

      

mainwindow.hh

#include <QMainWindow>

#include "myglwidget.hh"
#include "mainwindow_ui.hh" // generated by uic

class MainWindow : public QMainWindow, Ui_MainWindow
{

    MainWindow(QObject *parent = NULL) : 
        QMainWindow(parent)
    { // one would implement the constructor in the .cc file of course
        this->setupUi(this);

        glwidget = new MyGLWidget(this);

        // using the this pointer to emphase the location of the
        // member variable used.
        // NOTE: In the UI we defined a layout names verticalLayout
        this->verticalLayout->addWidget(glwidget);
    }

protected:
    MyGLWidget *glwidget;
};

      

The key is that you are only using the layout. A regular, regular UI layout to which you add your derived OpenGL widget. No morphs, no ui promotions!

+5


source







All Articles