Qt5.15.2新建QWidget工程
QT新建的去qmake工程的.pro文件设置
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++17
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
Osg_qt = G:/osgENV/osgQt
INCLUDEPATH += $${Osg_qt}/include \
G:/osgENV/osg365Vs2019x64/osg365/include
CONFIG(debug, debug|release){
LIBS += $${Osg_qt}/lib/osgQOpenGLd.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgd.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgViewerd.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgDBd.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgUtild.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/OpenThreadsd.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgGAd.lib \
}else
{
LIBS += $${Osg_qt}/lib/osgQOpenGL.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osg.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgViewer.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgDB.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgUtil.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/OpenThreads.lib \
G:/osgENV/osg365Vs2019x64/osg365/lib/osgGA.lib \
}
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgDB/ReadFile>
#include <osg/Texture2D>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
osgQOpenGLWidget* _pOsgWidget;
osg::ref_ptr<osgViewer::Viewer> _viewer;
signals:
private slots:
void initOsg();
void initOsgWindow();
};
#endif // WIDGET_H
widget.cpp文件
#include "widget.h"
#include <QHBoxLayout>
#include <osgDB/ReadFile>
#include <osgDB/Options>
#include <osgUtil/Optimizer>
using namespace osgDB;
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
// //方式一
// resize(800, 600);
// QHBoxLayout *pLayout = new QHBoxLayout(this);
// pLayout->setMargin(0);
// osgQOpenGLWidget *pOsgW = new osgQOpenGLWidget;
// pLayout->addWidget(pOsgW);
// connect(pOsgW, SIGNAL(initialized()), this, SLOT(initOsg()));
//方式二
_pOsgWidget = new osgQOpenGLWidget(this);
connect(_pOsgWidget, SIGNAL(initialized()), this, SLOT(initOsgWindow()));
QHBoxLayout * layout = new QHBoxLayout;
setLayout(layout);
layout->addWidget(_pOsgWidget);
}
Widget::~Widget()
{
}
void Widget::initOsg()
{
osgViewer::Viewer *pViewer = ((osgQOpenGLWidget *)sender())->getOsgViewer();
pViewer->setCameraManipulator(new osgGA::TrackballManipulator());
osg::Node *pNode = osgDB::readNodeFile("G:/osgENV/OpenSceneGraph_Data/cow.osg");
osgUtil::Optimizer optimizer;
optimizer.optimize(pNode);
pViewer->setSceneData(pNode);
}
void Widget::initOsgWindow()
{
_viewer = _pOsgWidget->getOsgViewer();
_viewer->getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
_viewer->setCameraManipulator(new osgGA::TrackballManipulator);
auto *option = new osgDB::Options("noTriStripPolygons");
Registry::instance()->setOptions(option);
_viewer->setSceneData(osgDB::readNodeFile("G:/osgENV/OpenSceneGraph_Data/cow.osg"));
}
main.cpp文件
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}