Is there an OpenGL Widget constructor for QT5?
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>&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 to share