首先声明:
需求是osg显示在ui界面上,然而这些资料参差不齐,看起来并不是很完善,因为opengl是osg的底层,所以想先尝试这将opengl绘制的图形显示在Qt的ui上,这方面的资料相对多一点。
本篇文章参考:https://blog.csdn.net/u014546553/article/details/74332153,给出的方法是窗口提升,也尝试了,最后也成功了,只不过走了一点弯路,文中给出的代码有些许错误,不可直接拿来用,我是在他的基础上进一步具体和完善。
Qt 5.13.2版本的mingw
步骤
-
新建工程 ,我这里命名为openglUi,类的名字MainWindow,继承QMainwindow,ui选上,下一步下一步就行;
-
Pro文件的修改 ;
a)加上opengl
b)文件末尾加上
win32:LIBS += -lOpengl32 \
-lglu32 \
下面来一个截图说明:
3.新建一个自定义widget,类名自定义为’‘openglwidget’’
4.添加控件
在mainwindow.ui上添加一个widget和一个pushButton,可以先不布局
-
把普通的’‘widget’‘的提升为自定义的openglwidget,右键选择菜单’‘提升为…’’ ;
-
自定义的openglwidget类,头文件和源文件的简单编写,其它的程序不用管它
a)openglwidget.h
#ifndef OPENGLWIDGET_H
#define OPENGLWIDGET_H
#include <QWidget>
#include <QOpenGLWidget>
#include <GL/gl.h>
#include <GL/glu.h>
namespace Ui {
class openglwidget;
}
class openglwidget : public QOpenGLWidget
{
Q_OBJECT
public:
explicit openglwidget(QWidget *parent = 0);
~openglwidget();
private:
Ui::openglwidget *ui;
protected:
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
};
#endif // OPENGLWIDGET_H
b)openglwidget.cpp
#include "openglwidget.h"
#include "ui_openglwidget.h"
openglwidget::openglwidget(QWidget *parent) :
QOpenGLWidget(parent),
ui(new Ui::openglwidget)
{
ui->setupUi(this);
}
openglwidget::~openglwidget()
{
delete ui;
}
void openglwidget::initializeGL()
{
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
void openglwidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
}
void openglwidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)w/h, 0.01, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
}
之后运行即可实现
为了更加完善,我将.pro也分享一下
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp \
openglwidget.cpp
HEADERS += \
mainwindow.h \
openglwidget.h
FORMS += \
mainwindow.ui \
openglwidget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32:LIBS += -lOpengl32 \
-lglu32 \