以前我在使用opengl创建qt应用程序时遇到了困难,我总是设法用SDL或glfw + glew创建一个纯粹的opengl应用程序。
但我按照每个人在视频中所做的程序,创建了一个派生自QOpenGLWidget的类,并将小部件提升为此类。
今天我决定以不同的方式做到这一点,它起作用,首先我创建了一个派生自QOpenGLWidget和QOpenGLFunctions的类,如in the qt documentation所述。
但是我没有宣传我的小部件,而是使用了ui->小部件在布局中获取它的引用并在我的类的构造函数中作为参数传递并且它起作用,至少它在部分工作得很好...
首先,我在我的窗口中添加了一个GridLayout,并在布局中引用了小部件以更好地适应用户的屏幕。然而,只有一个小部件被绘制,起初我认为opengl小部件正在改变我的小部件的大小,但我通过css添加了一个边框,看看它是否实际上是但是小部件是正常大小的根据图:
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);
}
答案 0 :(得分:1)
您忘了在m3dWorkspace
上添加布局。
添加一个,例如:
this->m_GLWorkspace = new GLWorkspace;
auto layout = new QVBoxLayout;
layout->addWidget(this->m_GLWorkspace);
ui->m3dWorkspace->setLayout(layout);