[转][osg][QT]osg与QT界面结合的简单例子
//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
[转][osg][QT]osg与QT界面结合的简单例子的更多相关文章
- [原][osg][QT]osg与QT界面结合的简单例子二
//main.cpp #include "VREObliqueEditorQTWindow.h" #include <QtWidgets/QApplication> # ...
- Robot Framework与Web界面自动化测试:简单例子
假设环境已经搭建好了.这里用RIDE( Robot Framework Test Data Editor)工具来编写用例.下面我们对Robot Framework简称rf. 我们先考虑下一个最基本的登 ...
- Qt osg QWidget osgViewer::Viewer
osgViewer::Viewer* _viewer = nullptr; _viewer = new osgViewer::Viewer;osg::ref_ptr<osg::Group> ...
- 用Qt写软件系列三:一个简单的系统工具之界面美化
前言 在上一篇中,我们基本上完成了主要功能的实现,剩下的一些导出.进程子模块信息等功能,留到后面再来慢慢实现.这一篇来讲述如何对主界面进行个性化的定制.Qt库提供的只是最基本的组件功能,使用这些组件开 ...
- qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新
qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新 Widget wit ...
- Qt 地址薄 (一) 界面设计
实现一个简单的地址薄,功能包括:地址的添加.浏览.编辑.查找.输出文件等. 1 界面和元素 整个地址薄界面,可视为一个 AddressBook 类.其中的 Name.Address 以及两个编辑栏, ...
- qml(Qt Quick)做界面
qml(Qt Quick)做界面 来源 https://www.zhihu.com/question/24880681/answer/29324824 本人是Qt初学者,正在写一个会计小软件(Lin ...
- [OpenCV Qt教程] 在Qt图形界面中显示OpenCV图像的OpenGL Widget(第二部分)
本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-opengl-widget-per-visualizzare-imm ...
- Qt常用的登录界面设计
记录一下Qt常用的登录界面的设计 方便以后使用! 1.QpushButton改变一个按钮的颜色,当鼠标放上去和移开时显示不同的颜色.QPushButton { background-color: rg ...
随机推荐
- 如何解决win10关机状态下,按键盘会自动开机的问题
关机状态下按下键盘会自动开机,是因为所装的系统默认设置了快速启动功能 下面是关闭快速启动的方法: 步骤一: 在win10桌面右击,点击显示设置 步骤二: 电源和睡眠-->其他电源设置 步骤三: ...
- php操作共享内存shmop类及简单使用测试(代码)
SimpleSHM 是一个较小的抽象层,用于使用 PHP 操作共享内存,支持以一种面向对象的方式轻松操作内存段.在编写使用共享内存进行存储的小型应用程序时,这个库可帮助创建非常简洁的代码.可以使用 3 ...
- Huffman Implementation with Python
Huffman Implementation with Python 码表 Token Frequency a 10 e 15 i 12 s 3 t 4 space 13 n 1 生成 Huffman ...
- 成绩统计程序(Java)
我的程序: package day20181018;/** * 成绩统计系统 * @author Administrator */import java.util.Scanner;//提供计算机直接扫 ...
- SSH服务拒绝了密码
原因一:密码错误 解决方法:修改密码 方式一:救援模式修改密码 方式二:单用户模式破解root密码 原因二: /etc/ssh/sshd_config里设置禁止root用户直接远程登录 解决方法: v ...
- 深刻理解Python中的元类(metaclass)以及元类实现单例模式
在理解元类之前,你需要先掌握Python中的类.Python中类的概念借鉴于Smalltalk,这显得有些奇特.在大多数编程语言中,类就是一组用来描述如何生成一个对象的代码段.在Python中这一点仍 ...
- linux如何使make输出makefile中所有的规则和变量
答: make -p (会执行makefile,加入-q可以阻止makefile的执行)
- linux下如何调试Makefile?
答:make --debug=all (输出所有的调试信息)
- POJ 1679 The Unique MST 【判断最小生成树是否唯一】
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Defini ...
- 解决/var/log下没有messages文件的问题?
fedora23和centos7+ 都是使用的 systemd 来代替sysv 管理系统启动和服务了. 在systemd 中主要包含两个方面的内容, 当打开/etc/inittab 文件时, 会看到: ...