原文链接
打开QT Creator,新建一个窗口项目。
QT版本如下:
修改pro文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \main.cpp \mainwindow.cpp
HEADERS += \mainwindow.h
FORMS += \mainwindow.ui
INCLUDEPATH += "D:\coding\osgearth\osgearth-vcpkg-installed\installed\x86-windows\include"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\include"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\build-win32\src"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\build-win32\include"
INCLUDEPATH += "D:\coding\osgearth\osgQt-master\build-win32\include"
LIBS += -L"D:\coding\osgearth\osgearth-vcpkg-installed\installed\x86-windows\lib" -losg -losgUtil -losgViewer -losgGA -losgDB -lOpenThreads -lopengl32
LIBS += -L"D:\coding\osgearth\osgQt-master\build-win32\lib" -losgQOpenGL
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
修改main.cpp文件
#include "mainwindow.h"#include <QApplication>
#include <osgQOpenGL/osgQOpenGLWidget>
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>
#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>
#include <osgGA/Device>
#include <QSurfaceFormat>
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char *argv[])
{QApplication a(argc, argv);osg::ArgumentParser arguments(&argc, argv);osgQOpenGLWidget widget(&arguments);QObject::connect(&widget, &osgQOpenGLWidget::initialized,[&widget]{osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;keyswitchManipulator->addMatrixManipulator('1', "Trackball", new osgGA::TrackballManipulator());//must has add one at leastwidget.getOsgViewer()->setCameraManipulator(keyswitchManipulator.get());//must has// load the dataosg::Node* nodePtr = osgDB::readNodeFile("D:/coding/osgearth/build-osgQtTest1-Desktop_Qt_5_14_2_MSVC2017_32bit-Release/release/cow.osgt") ;if(!nodePtr){std::cout << "No data loaded" << std::endl;return 1;}widget.getOsgViewer()->setSceneData(nodePtr);return 0;});MainWindow w;w.setCentralWidget(&widget);w.show();return a.exec();
}
以Release模式编译,拷贝上个笔记提到的dll到exe目录,注意要调用windeployqt解决部分qt依赖项。执行程序。