1. //QT += core gui opengl
  2. //LIBS += -losgViewer -losgDB -losgUtil -losg -lOpenThreads -losgGA -losgQt
  3. #include <QtGui/QApplication>
  4. #include <osg/ArgumentParser>
  5. #include <osgViewer/Viewer>
  6. #include <osgViewer/CompositeViewer>
  7. #include <osgViewer/ViewerEventHandlers>
  8. #include <osgGA/TrackballManipulator>
  9. #include <osgDB/ReadFile>
  10. #include <QtCore/QString>
  11. #include <QtCore/QTimer>
  12. #include <QtGui/QKeyEvent>
  13. #include <QtGui/QApplication>
  14. #include <QtOpenGL/QGLWidget>
  15. #include <QtGui/QMainWindow>
  16. #include <QtGui/QMdiSubWindow>
  17. #include <QtGui/QMdiArea>
  18. #include <iostream>
  19.  
  20. using Qt::WindowFlags;
  21.  
  22. class AdapterWidget:public QGLWidget
  23. {
  24. public:
  25. AdapterWidget(QWidget *parent=,const char* name=,const QGLWidget * shareWidget=,WindowFlags f=);
  26.  
  27. virtual ~AdapterWidget()
  28. {
  29.  
  30. }
  31.  
  32. osgViewer::GraphicsWindow* getGraphicsWindow()
  33. {
  34. return _gw.get();
  35. }
  36.  
  37. const osgViewer::GraphicsWindow* getGraphicsWidow()const
  38. {
  39. return _gw.get();
  40. }
  41. protected:
  42. void init();
  43. virtual void resizeGL(int width,int height);
  44. virtual void keyPressEvent(QKeyEvent* event);
  45. virtual void keyReleaseEvent(QKeyEvent* event);
  46. virtual void mousePressEvent(QMouseEvent* event);
  47. virtual void mouseReleaseEvent(QMouseEvent* event);//
  48. virtual void mouseMoveEvent(QMouseEvent* event);
  49.  
  50. osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> _gw;
  51.  
  52. };
  53.  
  54. AdapterWidget::AdapterWidget(QWidget *parent,const char* name,const QGLWidget * shareWidget,WindowFlags f):QGLWidget(parent,shareWidget,f)
  55. {
  56. _gw=new osgViewer::GraphicsWindowEmbedded(,,width(),height());
  57. setFocusPolicy(Qt::ClickFocus);
  58.  
  59. }
  60.  
  61. void AdapterWidget::resizeGL(int width, int height)
  62. {
  63. _gw->getEventQueue()->windowResize(,,width,height);
  64. _gw->resized(,,width,height);
  65.  
  66. }
  67. void AdapterWidget::keyPressEvent(QKeyEvent* event)
  68. {
  69. //_gw->getEventQueue()->keyPress( (osgGA::GUIEventAdapter::KeySymbol) *(event->text().toAscii().data() ) );
  70.   _gw->getEventQueue()->keyPress((osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));//qt5
  71. }
  72.  
  73. void AdapterWidget::keyReleaseEvent(QKeyEvent* event)
  74. {
  75. //_gw->getEventQueue()->keyRelease( (osgGA::GUIEventAdapter::KeySymbol)*(event->text().toAscii().data()));
  76.   _gw->getEventQueue()->keyRelease((osgGA::GUIEventAdapter::KeySymbol)*(event->text().toLatin1().data()));//qt5
  77. }
  78.  
  79. void AdapterWidget::mousePressEvent(QMouseEvent* event)
  80. {
  81. int button=;
  82. switch (event->button())
  83. {
  84. case(Qt::LeftButton):
  85. button=;
  86. break;
  87. case (Qt::MidButton):
  88. button=;
  89. break;
  90. case (Qt::RightButton):
  91. button=;
  92. break;
  93. case (Qt::NoButton):
  94. button=;
  95. break;
  96. default:
  97. button=;
  98. break;
  99.  
  100. }
  101.  
  102. _gw->getEventQueue()->mouseButtonPress(event->x(),event->y(),button);
  103.  
  104. }
  105.  
  106. void AdapterWidget::mouseReleaseEvent( QMouseEvent* event )
  107. {
  108. int button = ;
  109. switch(event->button())
  110. {
  111. case(Qt::LeftButton):
  112. button = ;
  113. break;
  114. case(Qt::MidButton):
  115. button = ;
  116. break;
  117. case(Qt::RightButton):
  118. button = ;
  119. break;
  120. case(Qt::NoButton):
  121. button = ;
  122. break;
  123. default:
  124. button = ;
  125. break;
  126. }
  127. _gw->getEventQueue()->mouseButtonRelease(event->x(), event->y(), button);
  128. }
  129.  
  130. void AdapterWidget::mouseMoveEvent(QMouseEvent* event)
  131. {
  132. _gw->getEventQueue()->mouseMotion(event->x(),event->y());
  133.  
  134. }
  135.  
  136. class ViewerQT : public osgViewer::Viewer, public AdapterWidget
  137. {
  138. public:
  139. ViewerQT(QWidget * parent=,const char * name=,const QGLWidget * shareWidget=,WindowFlags f=):AdapterWidget(parent ,name,shareWidget ,f)
  140. {
  141. getCamera()->setViewport(new osg::Viewport(,,width(),height()));
  142. getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(width())/static_cast<double>(height()), 1.0f, 10000.0f);
  143. getCamera()->setGraphicsContext(getGraphicsWindow());
  144.  
  145. setThreadingModel(osgViewer::Viewer::SingleThreaded);
  146. connect(&_timer,SIGNAL(timeout()),this,SLOT(updateGL()));//并且把它的timeout()连接到适当的槽。当这段时间过去了,它将会发射timeout()信号。
  147.  
  148. _timer.start();//使用start()来开始
  149.  
  150. }
  151.  
  152. virtual void paintGL()
  153. {
  154. frame();
  155. }
  156. protected:
  157. QTimer _timer;
  158. };
  159.  
  160. int main(int argc,char** argv)
  161. {
    /*

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的环境变量

  1. */
  2. QApplication a(argc,argv);
  3. osg::ref_ptr<osg::Node> loadedModel=osgDB::readNodeFile("cow.osg");
  4. ViewerQT * ViewerWindow=new ViewerQT;
  5. ViewerWindow->setCameraManipulator(new osgGA::TrackballManipulator);
  6. ViewerWindow->setSceneData(loadedModel.get());
  7.  
  8. QMainWindow* mw=new QMainWindow();
  9. mw->showMaximized();
  10. mw->setCentralWidget(ViewerWindow);
  11. mw->show();
  12. a.connect(&a,SIGNAL(lastWindowClosed()),&a,SLOT(quit()));
  13. return a.exec();
  14. }

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

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

[转][osg][QT]osg与QT界面结合的简单例子的更多相关文章

  1. [原][osg][QT]osg与QT界面结合的简单例子二

    //main.cpp #include "VREObliqueEditorQTWindow.h" #include <QtWidgets/QApplication> # ...

  2. Robot Framework与Web界面自动化测试:简单例子

    假设环境已经搭建好了.这里用RIDE( Robot Framework Test Data Editor)工具来编写用例.下面我们对Robot Framework简称rf. 我们先考虑下一个最基本的登 ...

  3. Qt osg QWidget osgViewer::Viewer

    osgViewer::Viewer* _viewer = nullptr; _viewer = new osgViewer::Viewer;osg::ref_ptr<osg::Group> ...

  4. 用Qt写软件系列三:一个简单的系统工具之界面美化

    前言 在上一篇中,我们基本上完成了主要功能的实现,剩下的一些导出.进程子模块信息等功能,留到后面再来慢慢实现.这一篇来讲述如何对主界面进行个性化的定制.Qt库提供的只是最基本的组件功能,使用这些组件开 ...

  5. qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新

    qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新 Widget wit ...

  6. Qt 地址薄 (一) 界面设计

    实现一个简单的地址薄,功能包括:地址的添加.浏览.编辑.查找.输出文件等. 1  界面和元素 整个地址薄界面,可视为一个 AddressBook 类.其中的 Name.Address 以及两个编辑栏, ...

  7. qml(Qt Quick)做界面

    qml(Qt Quick)做界面 来源  https://www.zhihu.com/question/24880681/answer/29324824 本人是Qt初学者,正在写一个会计小软件(Lin ...

  8. [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)

    本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...

  9. Qt常用的登录界面设计

    记录一下Qt常用的登录界面的设计 方便以后使用! 1.QpushButton改变一个按钮的颜色,当鼠标放上去和移开时显示不同的颜色.QPushButton { background-color: rg ...

随机推荐

  1. ManyToManyField 增加记录

    class BOMView(View): def get(self,request): obj=BOMForm() return render(request,'bom.html',{'obj':ob ...

  2. 小程序 canvas实现图片预览,图片保存

    wxml 代码: <view class="result-page"> <canvas bindtap="previewImage" canv ...

  3. yii2项目中运行composer 过程中遇到的问题

    问题1: Your requirements could not be resolved to an installable set of packages 则表明 未安装fxp/composer-a ...

  4. 13:python-ldap

    1.1 python-ldap 基本使用 11111111111111111111

  5. linux 计划任务 crontab 简单用法

    添加计划任务: 方法1: crontab -e 方法2: vim /etc/crontab #这种方式进去的文件有个sample可供参考 查看已经启动的任务:crontab -l 查看运行状态:ser ...

  6. centos 设置时间为北京时间

    https://www.cnblogs.com/biaopei/p/7730462.html

  7. 数据库 --- 4 多表查询 ,Navicat工具 , pymysql模块

    一.多表查询 1.笛卡儿积 查询 2.连接 语法: ①inner    显示可构成连接的数据 mysql> select employee.id,employee.name,department ...

  8. Android灯光系统--通知灯深入分析【转】

    本文转自:https://www.cnblogs.com/lkq1220/p/6406261.html Android灯光系统--通知灯深入分析 通知的类别 声音 振动 闪灯 APP如何发出通知灯请求 ...

  9. bzoj1458: 士兵占领 网络流

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1458 也可以去luogu 思路 想成倒着删去点,使得依旧满足覆盖!! 左边横,右边列,之间用 ...

  10. Docker Tomcat远程部署到容器

    一:创建一个开启远程部署的tomcat容器 tomcat角色配置 1.tomcat开启远程部署,修改conf/tomcat-users.xml <?xml version="1.0&q ...