gpt4 book ai didi

c++ - QOpenGLWidget 不在整个小部件中呈现

转载 作者:行者123 更新时间:2023-11-30 05:00:54 24 4
gpt4 key购买 nike

以前我用 opengl 创建 qt 应用程序有困难,我总是设法用 SDL 或 glfw + glew 创建一个纯 opengl 应用程序。

但我按照大家在视频中所做的程序,创建了一个派生自 QOpenGLWidget 的类,并将一个小部件提升到该类。

今天我决定以不同的方式来做它并且它起作用了,首先我创建了一个派生自 QOpenGLWidget 和 QOpenGLFunctions 的类,如所述 in the qt documentation .

但是我没有提升我的小部件,而是使用 ui-> 小部件在布局中获取它的引用并作为参数传递到我的类的构造函数中并且它起作用了,至少它在某些部分运行良好......

首先,我将 GridLayout 添加到我的窗口和在布局中引用的小部件,以更好地适应用户的屏幕。但是只有一个小部件被绘制,起初我以为是 opengl 小部件正在改变我的小部件的大小,但是我通过 css 添加了一个边框以查看它是否真的是那个但是小部件是正常大小,如图所示:

My window

主窗口.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>&amp;Settings</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

主窗口.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);
}

最佳答案

您忘记在 m3dWorkspace 上添加布局。添加一个,例如:

this->m_GLWorkspace = new GLWorkspace;
auto layout = new QVBoxLayout;
layout->addWidget(this->m_GLWorkspace);
ui->m3dWorkspace->setLayout(layout);

关于c++ - QOpenGLWidget 不在整个小部件中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50511165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com