Reputation: 345
Previously I had difficulties creating a qt application with opengl, I always managed to create a pure opengl application with either SDL or glfw + glew.
But I followed the procedure everyone did in videos, created a class derived from QOpenGLWidget and promoted a widget to this class.
Today I decided to do it differently and it worked, first I created a class derived from QOpenGLWidget and QOpenGLFunctions as described in the qt documentation.
But instead of promoting my widget I used ui-> widget to get its reference in the layout and pass as parameter in the constructor of my class and it worked, at least it worked out well in parts ...
First of all I added a GridLayout to my window and the widget that was referenced inside the layout to better fit the user's screen. However only one piece of the widget is painted, at first I thought the opengl widget was changing the size of my widget, but I added a border through css to see if it was actually that but the widget was in the normal size according to figure:
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>656</width>
<height>350</height>
</rect>
</property>
<property name="windowTitle">
<string>Open 3D Texture Painter</string>
</property>
<property name="styleSheet">
<string notr="true">#m3dWorkspace{
border: 3px solid red;
}</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QWidget" name="m3dWorkspace" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>656</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
<addaction name="action_Settings"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="action_Settings">
<property name="text">
<string>&Settings</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
mainwindow.cpp
#include <QSettings>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "glworkspace.h"
#include "settings_window.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSettings settings(qApp->applicationDirPath() + "//settings.ini", QSettings::IniFormat);
settings.beginGroup("Render");
if(settings.value("renderApi", "ogl").toString() == "ogl"){
this->m_GLWorkspace = new GLWorkspace(ui->m3dWorkspace);
}else if(settings.value("renderApi", "ogl").toString() == "d3dx"){
///@todo setup render with DirectX
}else if(settings.value("renderApi", "ogl").toString() == "vk"){
///@todo setup render with vulkan
}
settings.endGroup();
this->m_SettingsWin = new SettingsWindow(this);
}
MainWindow::~MainWindow()
{
delete m_SettingsWin;
if(m_GLWorkspace != NULL){
delete m_GLWorkspace;
}
delete ui;
}
void MainWindow::on_action_Settings_triggered()
{
m_SettingsWin->exec();
}
glworkspace.h
#ifndef GLWORKSPACE_H
#define GLWORKSPACE_H
#include <QtOpenGL>
class GLWorkspace : public QOpenGLWidget, protected QOpenGLFunctions{
Q_OBJECT
public:
explicit GLWorkspace(QWidget *parent=0);
~GLWorkspace();
protected:
virtual void initializeGL() Q_DECL_OVERRIDE;
virtual void resizeGL(int w, int h) Q_DECL_OVERRIDE;
virtual void paintGL() Q_DECL_OVERRIDE;
private:
QOpenGLFunctions *m_F;
};
#endif // GLWORKSPACE_H
glwokspace.cpp
#include "glworkspace.h"
GLWorkspace::GLWorkspace(QWidget *parent) :
QOpenGLWidget(parent)
{
QSurfaceFormat format;
format.setVersion(4, 3);
format.setProfile(QSurfaceFormat::CoreProfile);
setFormat(format);
}
GLWorkspace::~GLWorkspace()
{
}
void GLWorkspace::initializeGL()
{
initializeOpenGLFunctions();
this->m_F = QOpenGLContext::currentContext()->functions();
m_F->glClearColor(0.0f, 1.0f, 0.5f, 0.0f);
}
void GLWorkspace::resizeGL(int w, int h)
{
m_F->glViewport(0, 0, w, h);
}
void GLWorkspace::paintGL()
{
m_F->glClear(GL_COLOR_BUFFER_BIT);
}
Upvotes: 1
Views: 981
Reputation: 541
You forgot to add a layout on m3dWorkspace
.
Add one, for example:
this->m_GLWorkspace = new GLWorkspace;
auto layout = new QVBoxLayout;
layout->addWidget(this->m_GLWorkspace);
ui->m3dWorkspace->setLayout(layout);
Upvotes: 1