Qt窗口嵌入

本文介绍如何在Qt中实现窗口嵌入,通过创建CustomProxy类继承自QGraphicsProxyWidget,并重写相关事件,利用QGraphicsView和QGraphicsScene将窗口组件无缝集成到图形场景中。
摘要由CSDN通过智能技术生成

创建一个QGraphicsProxyWidget 的子类CustomProxy 

class CustomProxy : public QGraphicsProxyWidget 

在子类中重写事件

#ifndef CUSTOMPROXY_H
#define CUSTOMPROXY_H

#include <QTimeLine>
#include <QGraphicsProxyWidget>

class CustomProxy : public QGraphicsProxyWidget
{
    Q_OBJECT

public:
    explicit CustomProxy(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);

    QRectF boundingRect() const Q_DECL_OVERRIDE;
    void paintWindowFrame(QPainter *painter, const QStyleOptionGraphicsItem *option,
                   
要将OSG模型嵌入Qt窗口中,可以使用Qt的OpenGL模块和OSG的Qt插件。具体步骤如下: 1. 创建一个Qt窗口,继承自QOpenGLWidget。 2. 在窗口的初始化函数中,创建一个OSG Viewer对象,并将其设置为OpenGL上下文。 3. 在窗口的paintGL()函数中,调用OSG的渲染函数进行渲染。 4. 在窗口的resizeGL()函数中,更新OSG Viewer的视口大小。 5. 在Qt的主函数中,加载OSG的Qt插件,以便在Qt应用程序中使用OSG。 下面是一个简单的示例代码: ```cpp #include <osgViewer/Viewer> #include <osgQt/GraphicsWindowQt> class OSGWidget : public QOpenGLWidget { public: OSGWidget(QWidget* parent = nullptr) : QOpenGLWidget(parent) { osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits(); traits->windowName = "OSG Widget"; traits->x = x(); traits->y = y(); traits->width = width(); traits->height = height(); traits->windowDecoration = false; traits->doubleBuffer = true; traits->sharedContext = 0; osgQt::GraphicsWindowQt* gw = new osgQt::GraphicsWindowQt(traits.get()); osg::Camera* camera = new osg::Camera; camera->setGraphicsContext(gw); camera->setViewport(new osg::Viewport(0, 0, width(), height())); setCameraManipulator(new osgGA::TrackballManipulator); viewer.setCamera(camera); viewer.setThreadingModel(osgViewer::Viewer::SingleThreaded); } protected: void paintGL() override { viewer.frame(); } void resizeGL(int w, int h) override { viewer.getCamera()->setViewport(new osg::Viewport(0, 0, w, h)); } private: osgViewer::Viewer viewer; }; int main(int argc, char** argv) { QApplication app(argc, argv); osgDB::Registry::instance()->loadLibrary("osgdb_qt.so"); OSGWidget widget; widget.resize(640, 480); widget.show(); return app.exec(); } ``` 这里假设OSG已经正确安装,并且Qt应用程序已经配置好了OpenGL环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值