在学习QT的过程中发现有一个特别炫酷的行星例子“planets”,有两种实现版本:

一种是基于Qt 3D QML(planets-qml),另一种则是基于Quick和强大的Three.js(planets)。

在主函数调用过程中,这两个例子均使用QQuickView类来加载qml文件。

QQuickView嵌入到QT MDI中

将QMainWindow的中间窗口设置为多文档窗口区域:

 C++ Code 
1
2
3
4
 
QMdiArea* pCentralWidget = new QMdiArea;
setCentralWidget(pCentralWidget);
pCentralWidget->addSubWindow(pWidget);
pWidget->show();

其中关键是调用addSubWindow方法来不断增加子窗口,参数为QWidget指针。

QMdiSubWindow *QMdiArea::addSubWindow(QWidget *widgetQt::WindowFlags windowFlags = ...)

经过测试发现,我使用QQuickWidget来替代QQuickView,planets-aml例子可以正常显示,但是planets例子却显示不出来,可能是该类不支持加载Three.js?

最后我将QQuickView包装成QWidget可以成功加载,做了一个MDI的例子:

在planets的副本上进行改造:

MainWindow.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QMdiArea>
#include <QQuickView>

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = nullptr);

signals:

public slots:
    void newFile();

private:
    QMdiArea*       m_pCentralWidget;
    QQuickView*     newPlanet3D();
};

#endif // MAINWINDOW_H

MainWindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
#include "MainWindow.h"
#include <QMenu>
#include <QMenuBar>
#include <QCoreApplication>
#include <QtQml/QQmlEngine>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    m_pCentralWidget = new QMdiArea;
    setCentralWidget(m_pCentralWidget);
    QMenu *fileMenu = menuBar()->addMenu(QObject::tr("&File"));
    fileMenu->addAction(QObject::tr("N&ew"), this, SLOT(newFile()));
    fileMenu->addAction(QObject::tr("E&xit"), qApp, &QCoreApplication::quit);
}

void MainWindow::newFile()
{
    QWidget *container = QWidget::createWindowContainer(newPlanet3D());
    container->setMinimumSize();
    m_pCentralWidget->addSubWindow(container);
    container->show();
}

QQuickView* MainWindow::newPlanet3D()
{
    QQuickView* viewer = new QQuickView;
    // The following are needed to make examples run without having to install the module
    // in desktop environments.
#ifdef Q_OS_WIN
    QString extraImportPath(QStringLiteral("%1/../../../../%2"));
#else
    QString extraImportPath(QStringLiteral("%1/../../../%2"));
#endif
    viewer->engine()->addImportPath(extraImportPath.arg(QCoreApplication::applicationDirPath(),
                                                        QString::fromLatin1("qml")));
    viewer->setSource(QUrl("qrc:/planets.qml"));
    viewer->setTitle(QStringLiteral("Qt Canvas 3D Examples - Planets"));
    viewer->setResizeMode(QQuickView::SizeRootObjectToView);
    viewer->setColor(Qt::black);

return viewer;
}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
#include <QApplication>
#include "MainWindow.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

MainWindow mainWindow;
    mainWindow.resize();
    mainWindow.show();

return app.exec();
}

编译运行,“File”->“New”新建行星子窗口,“File”->“Exit”退出应用。行星子窗口中显示以及事件响应均正常!

QT QQuickView嵌入到QT MDI中的更多相关文章

  1. 对《将Unreal4打包后的工程嵌入到Qt或者桌面中》一文的补充

    在上一文中本人尝试将Ue4嵌入到Qt中,但依然有一些问题没有去尝试解决.今天因为帮助知乎专栏作者@大钊的关系,顺便进行补完. 2018.7.18更新: 正好在参加杭州UnrealCircle的时候见到 ...

  2. 将Unreal4打包后的工程嵌入到Qt或者桌面中

    #include "widget.h" #include "ui_widget.h" #include "windows.h" #inclu ...

  3. Qt实现嵌入桌面的半透明窗口 good

    这儿用上了前面一文提到的函数findDesktopIconWnd().见: http://mypyg.blog.51cto.com/820446/263349 一.将Qt窗口嵌入到桌面中.声明一个最简 ...

  4. qt cef嵌入web(二)

    在qt cef嵌入web文章中已经讲述了怎么把cef页面嵌入到qt程序中,但是这样并不完美,因为如果需要在多个窗口上创建cef浏览器部件的话,在 消息监听部分没有办法做区分多个浏览器事件,在这篇文章中 ...

  5. Qt Examples - Boxes (在Qt场景视图中结合OpenGL渲染)

    QT自带例程Boxes使用QT Graphics View框架实现了2D图形和3D图形的混合渲染,综合性比较强,整合知识较多,值得学习. 可以使用鼠标通过以下方式控制演示中的元素: 按住鼠标左键的同时 ...

  6. 【Qt开发】关于Qt应用程序中的堆栈、静态存储区的使用错误

    [Qt开发]关于Qt应用程序中的堆栈.静态存储区的使用错误 标签:[Qt开发] 最近终于又碰到了这个问题,想在main函数中定义一个局部大的数组,结果运行就报错,尼玛!刚开始真的不知道到发生了什么,后 ...

  7. PyQt(Python+Qt)学习随笔:Qt Designer中部件的autoFillBackground属性

    autoFillBackground属性可以确认部件背景是否自动填充,如果自动填充,Qt会在调用Paint事件之前填充部件的背景.使用的颜色由部件调色板中的QPalette.window 角色定义(关 ...

  8. PyQt(Python+Qt)学习随笔:Qt Designer中部件的accessibleDescription和accessibleName辅助阅读属性

    accessibleDescription和accessibleName属性都是用于残疾人辅助阅读的,这两个属性都有国际化属性(关于国际化请参考<PyQt(Python+Qt)学习随笔:Qt D ...

  9. PyQt(Python+Qt)学习随笔:Qt Designer中部件的toolTip、toolTipDuration、statusTip、whatsThis属性

    toolTip属性 toolTip属性设置部件的toolTip提示信息,toolTip提示信息在鼠标放到控件上会浮动出一个小框显示提示信息.默认情况下,仅显示活动窗口子部件的toolTip,可以通过在 ...

随机推荐

  1. JavaScript_几种创建对象(2017-07-04)

    理解对象 1.创建Object实例 var clock = new Object(); clock.hour = 12; clock.minute = 10; clock.showHour = fun ...

  2. 如何正确地使用android中的progressdialog

    网上有很多关于progressdialog的用法的介绍,下面这个是最具代表性的: http://sd8089730.iteye.com/blog/1441610 其核心代码: Handler hand ...

  3. PLSQL连接Oracle 数据库配置详解

    1. 下载instantclient-basic-win32-11.2.0.1.0 (oracle官网下载地址:http://www.oracle.com/technetwork/topics/win ...

  4. ELK菜鸟手记 (一) 环境配置+log4j日志记录

    1. 背景介绍 在大数据时代,日志记录和管理变得尤为重要. 以往的文件记录日志的形式,既查询起来又不方便,又造成日志在服务器上分散存储,管理起来相当麻烦, 想根据一个关键字查询日志中某个关键信息相当困 ...

  5. Java字节码 小结

    Reference javap 基本使用方法 深入理解java字节码 从Java代码到字节码 Java字节码.class文件案例分析 字节码 核心概念 Class文件是8位字节流,按字节对齐.之所以称 ...

  6. 【Zookeeper】源码分析之服务器(四)之FollowerZooKeeperServer

    一.前言 前面分析了LeaderZooKeeperServer,接着分析FollowerZooKeeperServer. 二.FollowerZooKeeperServer源码分析 2.1 类的继承关 ...

  7. C#后台执行js

    StringBuilder sb = new StringBuilder(); sb.Append("<script type='text/javascript'>") ...

  8. 【AI】Exponential Stochastic Cellular Automata for Massively Parallel Inference - 大规模并行推理的指数随机元胞自动机

    [论文标题]Exponential Stochastic Cellular Automata for Massively Parallel Inference     (19th-ICAIS,PMLR ...

  9. producter-consumer 他山之石

    #include <pthread.h> #include <list> using namespace std; template <typename T> cl ...

  10. CentOS7.4安装配置mysql8 TAR免安装版

    下载mysql: https://dev.mysql.com/downloads/mysql/ 解压tar.xz文件:先 xz -d mysql-8.0.15-linux-glibc2.12-x86_ ...