目录

官方解析

博主例子


官方解析

QQmlApplicationEngine提供了从一个QML文件里面加载应用程序的方式。
这类联合了QQmlEngine和QmlComponent去加载单独的QML文件。他还向QML提供了应用程序的功能,这个应用程序的功能能够让C++与QML混合编程,使用C++控制业务逻辑,用QML做界面:
官方给的用法:

  QQuickWidget *view = new QQuickWidget;view->setSource(QUrl::fromLocalFile("myqmlfile.qml"));view->show();

他这个不像QQuickView,QQmlApplicationEngine那样,他不会自动创建一个根窗口,如果你使用Qt Quick里面的视觉item,你需要把qml里面加一个Window,然后将其放进去。
如果不使用需要QGuiApplication的QML模块(比如QtQuick)可以使用QCoreApplication与QQmlApplicationEngine。
下面列出了默认QQmlEngine关联了的配置列表:
1.QML中的Qt.quit()与QCoreApplication::quit()关联在了一起;
2.自动从主QML文件附近加载翻译文件;
3.如果存在QQuickWIndow就会自动设置an incubation controller;
4.会自动设置QQmlFileSelector作为url拦截,应用于QML文件选择;
通过继承QQmlEngine可以更好的实现

博主例子

下面来补充下官方的例子(官方只给出了3行代码现在把他完善起来)

完善后包含如下内容:

qml与C++混合编程及QQuick与widgets混合

做一个qml实现的表格,C++来控制业务,程序运行截图如下:

程序结构图如下:

源码如下:

mytableviewmodel.h

#ifndef MYTABLEVIEWMODEL_H
#define MYTABLEVIEWMODEL_H#include <QAbstractTableModel>class MyTableViewModel: public QAbstractTableModel
{Q_OBJECT
public:MyTableViewModel();int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;QHash<int, QByteArray> roleNames() const Q_DECL_OVERRIDE;Q_INVOKABLE void addItem(QString one, QString two, QString three);private:QVector<QVector<QString>> m_aryData;};#endif // MYTABLEVIEWMODEL_H

main.cpp

#include <QGuiApplication>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QWidget>
#include <QQuickView>
#include "mytableviewmodel.h"int main(int argc, char *argv[])
{QGuiApplication app(argc, argv);QQmlApplicationEngine engine;MyTableViewModel model;model.addItem(QStringLiteral("中文"), QStringLiteral("999"), QStringLiteral("888"));engine.rootContext()->setContextProperty("theModel", &model);engine.load(QUrl(QLatin1String("qrc:/main.qml")));return app.exec();
}

mytableviewmodel.cpp

#include "mytableviewmodel.h"MyTableViewModel::MyTableViewModel(): QAbstractTableModel(NULL)
{}int MyTableViewModel::rowCount(const QModelIndex &parent) const
{Q_UNUSED(parent)return m_aryData.size();
}int MyTableViewModel::columnCount(const QModelIndex &parent) const
{Q_UNUSED(parent)return 3;
}QVariant MyTableViewModel::data(const QModelIndex &index, int role) const
{return m_aryData[index.row()][role];
}QHash<int, QByteArray> MyTableViewModel::roleNames() const
{QHash<int, QByteArray> roles;roles[0] = "1";roles[1] = "2";roles[2] = "3";return roles;
}void MyTableViewModel::addItem(QString one, QString two, QString three)
{beginInsertRows(QModelIndex(), m_aryData.size(),m_aryData.size());QVector<QString> list;list << one << two << three;m_aryData << list;endInsertRows();
}

main.qml

import QtQuick 2.8
import QtQuick.Controls 2.1ApplicationWindow {//id: frmWindow//width: 400//height: 300visible: trueMyTableView{id: myTableViewheight: parent.heightwidth: parent.widthfocus: falsetableView.itemDelegate:Rectangle {TextField{id: textFieldheight: 25text: styleData.value}}tableView.rowDelegate: Rectangle {height: 25}}
}

MyTableView.qml

import QtQuick 2.8
import QtQuick.Controls 1.4TableView {property alias tableView: tableViewid: tableViewTableViewColumn {title: "第一列"; role: "1"; width: 120}TableViewColumn {title: "第二列"; role: "2"; width: 120}TableViewColumn {title: "第三列"; role: "3"; width: 120}model: theModel
}

如果要用qml和传统widgets混合,因为QQuickView已经提供了转化为QWidget的接口,所以很简单,修改如下代码:

main.cpp

#include <QGuiApplication>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QWidget>
#include <QQuickView>
#include "mytableviewmodel.h"int main(int argc, char *argv[])
{//与传统widgets结合QApplication app(argc, argv);QQuickView view;MyTableViewModel model;model.addItem(QStringLiteral("中文"), QStringLiteral("999"), QStringLiteral("888"));view.engine()->rootContext()->setContextProperty("theModel", &model);view.setResizeMode(QQuickView::SizeRootObjectToView);view.setSource(QUrl("qrc:/main.qml"));QWidget *widget = QWidget::createWindowContainer(&view);widget->show();return app.exec();
}

main.qml

import QtQuick 2.8
import QtQuick.Controls 2.1Item {//id: frmWindow//width: 400//height: 300visible: trueMyTableView{id: myTableViewheight: parent.heightwidth: parent.widthfocus: falsetableView.itemDelegate:Rectangle {TextField{id: textFieldheight: 25text: styleData.value}}tableView.rowDelegate: Rectangle {height: 25}}
}

Qt文档阅读笔记-QQmlApplicationEngine解析与实例(qml与C++混合编程及QQuick与widgets混合)相关推荐

  1. Qt文档阅读笔记-Q_PROPERTY解析及实例

    目录 官方解析 博主栗子 官方解析 这个宏用于继承于QObject的类声明属性.这样声明后的属性行为与类数据成员一样,但是他们可以通过元对象系统进行访问. Q_PROPERTY(type name(R ...

  2. Qt文档阅读笔记-QScopedPointer解析及实例

    当指针超出范围后就会删除被引用的对象. 与QPointer不同,他可以在任意类型中使用(QPointer只能在identity type中使用) 4个不同的清除类         1. QScoped ...

  3. Qt文档阅读笔记-Fortune Client Example实例解析

    目录 官方解析 实例代码 博主增加解析 官方解析 Fortune Client Example 以使用QTcpSocket为例子,服务端可以配合Fortune Server或Threaded Fort ...

  4. Qt文档阅读笔记-Multiple Inheritance Example 实例解析及Automatic Connections解析

    目录 Multiple Inheritance Example 实例解析 Automatic Connections解析 Multiple Inheritance Example 实例解析 这个实例很 ...

  5. Qt文档阅读笔记-QIODevice解析及Audio Example实例解析

    目录 QIODevice官方解释及个人分析 Audio Example官方实例解析 QIODevice官方解释及个人分析 QIODevice类是Qt中I/O设备的接口. 提供了读和写的接口,QIODe ...

  6. Qt文档阅读笔记-Transformations解析及例子

    目录 官方解析 博主小栗子 官方解析 QGraphicsItem除了基于他的坐标pos()外,还支持投影变化.下面提供了几种变化item的方式.下面来说明下简单的转换,可以通过调用setRotatio ...

  7. Qt文档阅读笔记-QLatin1String解析及文本段跨行赋值

    QString的许多成员函数都被重载了,用于接收const char *.包括拷贝构造函数.分配符.操作符 insert().replace().indexOf().上述的函数都被优化避免const ...

  8. Qt文档阅读笔记-共享库的创建与调用

    使用共享库的符号 这个符号可以作用在变量.类.函数中,并且这些都可以被调用端使用. 在编译共享库中,需要使用export符号.在使用端调用的时候使用import符号. 这里是本人从文档中记录的笔记,大 ...

  9. Qt文档阅读笔记-加载HeightMap(高度图)构造3D地形图

    Qt文档阅读笔记-加载HeightMap(高度图)构造3D地形图 QHeightMapSurfaceDataProxy:是Q3DSurface的一个基本代理类. 他是专门加载高度图. 高度图是没有X, ...

最新文章

  1. 《linux核心应用命令速查》连载十四:fuser:用文件或者套接口表示进程
  2. C# VC HTTP POST GET(转)
  3. keepalived 主从配置日志报错:one or more vip associated with vrid mismatch actual master advert...
  4. SHA1加密(简单)
  5. linux-搜索查找类
  6. Linux系统(五)负载均衡LVS集群之DR模式
  7. python的empty函数_python中numpy.empty()函数的用法
  8. Android疯狂ListView之旅 第一季 《侧滑删除条目》
  9. c语言设置bufsiz大小,c语言缓冲区有关问题及c++中的this指针
  10. [JS+CSS] - 新浪微博滚动特效[兼容FF,Chrome和IE6,7,8]
  11. 测试自己幸运数字的软件,心理测试:4个数字哪个是你的幸运数字?测试你的心理年龄!...
  12. 「Python编程规范」语句分隔符号
  13. word文档下面有红色波浪线如何解决
  14. c#.net开发金融行情分析软件k线图蜡烛图之布林线指标计算
  15. 分享全球范围内比较好用的17个免费空间(基本稳定好多年)
  16. 为你的掘金和 GitHub 设定个性域名
  17. 剑指 Offer 12-20
  18. 红宝书初步研读随手笔记
  19. 2018-9-3-vue的tabs表单代码的思考
  20. 人脸验证:DeepID(转)

热门文章

  1. 社会工程学***的八种常用方法
  2. SEO|搜索引擎优化(如何让你的网站排名靠前)
  3. 程序员真正的天赋是什么?
  4. 产品经理有话说!这个报表神器更新了6大功能,绝对亮眼
  5. 什么是Python中的map,reduce和filter?
  6. 判断无向图是否有回路有四种方法
  7. 飞鸽传书2013年开发计划
  8. 网络管理员如何解放自己
  9. *printf()格式化串安全漏洞分析(下)
  10. 易企秀 伪静态 linux,易企秀无法预览(伪静态配置问题)!