[转][osg][QT]osg与QT界面结合的简单例子

时间:2023-01-10 11:07:58
//QT += core gui opengl
//LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt
#include <QtGui/QApplication>
#include <osg/ArgumentParser>
#include <osgViewer/Viewer>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <QtCore/QString>
#include <QtCore/QTimer>
#include <QtGui/QKeyEvent>
#include <QtGui/QApplication>
#include <QtOpenGL/QGLWidget>
#include <QtGui/QMainWindow>
#include <QtGui/QMdiSubWindow>
#include <QtGui/QMdiArea>
#include <iostream> using Qt::WindowFlags; class AdapterWidget:public QGLWidget
{
public:
AdapterWidget(QWidget *parent=,const char* name=,const QGLWidget * shareWidget=,WindowFlags f=); virtual ~AdapterWidget()
{ } osgViewer::GraphicsWindow* getGraphicsWindow()
{
return _gw.get();
} const osgViewer::GraphicsWindow* getGraphicsWidow()const
{
return _gw.get();
}
protected:
void init();
virtual void resizeGL(int width,int height);
virtual void keyPressEvent(QKeyEvent* event);
virtual void keyReleaseEvent(QKeyEvent* event);
virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event);//
virtual void mouseMoveEvent(QMouseEvent* event); osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw; }; AdapterWidget::AdapterWidget(QWidget *parent,const char* name,const QGLWidget * shareWidget,WindowFlags f):QGLWidget(parent,shareWidget,f)
{
_gw=new osgViewer::GraphicsWindowEmbedded(,,width(),height());
setFocusPolicy(Qt::ClickFocus); } void AdapterWidget::resizeGL(int width, int height)
{
_gw->getEventQueue()->windowResize(,,width,height);
_gw->resized(,,width,height); }
void AdapterWidget::keyPressEvent(QKeyEvent* event)
{
//_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
  _gw->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));//qt5
} void AdapterWidget::keyReleaseEvent(QKeyEvent* event)
{
//_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toAscii().data()));
  _gw->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));//qt5
} void AdapterWidget::mousePressEvent(QMouseEvent* event)
{
int button=;
switch (event->button())
{
case(Qt::LeftButton):
button=;
break;
case (Qt::MidButton):
button=;
break;
case (Qt::RightButton):
button=;
break;
case (Qt::NoButton):
button=;
break;
default:
button=;
break; } _gw->getEventQueue()->mouseButtonPress(event->x(),event->y(),button); } void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
{
int button = ;
switch(event->button())
{
case(Qt::LeftButton):
button = ;
break;
case(Qt::MidButton):
button = ;
break;
case(Qt::RightButton):
button = ;
break;
case(Qt::NoButton):
button = ;
break;
default:
button = ;
break;
}
_gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
} void AdapterWidget::mouseMoveEvent(QMouseEvent* event)
{
_gw->getEventQueue()->mouseMotion(event->x(),event->y()); } class ViewerQT : public osgViewer::Viewer, public AdapterWidget
{
public:
ViewerQT(QWidget * parent=,const char * name=,const QGLWidget * shareWidget=,WindowFlags f=):AdapterWidget(parent ,name,shareWidget ,f)
{
getCamera()->setViewport(new osg::Viewport(,,width(),height()));
getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
getCamera()->setGraphicsContext(getGraphicsWindow()); setThreadingModel(osgViewer::Viewer::SingleThreaded);
connect(&_timer,SIGNAL(timeout()),this,SLOT(updateGL()));//并且把它的timeout()连接到适当的槽。当这段时间过去了,它将会发射timeout()信号。 _timer.start();//使用start()来开始 } virtual void paintGL()
{
frame();
}
protected:
QTimer _timer;
}; int main(int argc,char** argv)
{
/*

QTextCodec *xcodec = QTextCodec::codecForLocale();
QString exeDir = xcodec->toUnicode(QByteArray(argv[0]));
QString BKE_CURRENT_DIR = QFileInfo(exeDir).path();
QStringList libpath;
libpath << BKE_CURRENT_DIR + QString::fromLocal8Bit("/plugins/platforms");
libpath << BKE_CURRENT_DIR << BKE_CURRENT_DIR + QString::fromLocal8Bit("/plugins/imageformats");
libpath << BKE_CURRENT_DIR + QString::fromLocal8Bit("/plugins");
libpath << QApplication::libraryPaths();
QApplication::setLibraryPaths(libpath);
//以上是跑qt的环境变量

*/
QApplication a(argc,argv);
osg::ref_ptr<osg::Node> loadedModel=osgDB::readNodeFile("cow.osg");
ViewerQT * ViewerWindow=new ViewerQT;
ViewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
ViewerWindow->setSceneData(loadedModel.get()); QMainWindow* mw=new QMainWindow();
mw->showMaximized();
mw->setCentralWidget(ViewerWindow);
mw->show();
a.connect(&a,SIGNAL(lastWindowClosed()),&a,SLOT(quit()));
return a.exec();
}

转自:https://www.cnblogs.com/sangzaohaishui/p/4687771.html

注意:QGLWidget使用的库是:Qt5OpenGL.lib