当前位置:网站首页>QML和QWidget混合开发(初探)
QML和QWidget混合开发(初探)
2022-07-06 04:10:00 【BUG_C++】
为什么要搞混合开发?
Qml已经越来越成为Qt开发的主流,相比与QWidget的界面开发更快,也更容易上手,实现效果上也更好。但老旧项目都是QWidget的框架,大家不可能一次性的把QWidget项目界面全部换成qml,这时候我们可以将新开发界面用qml。也可以用qml做一些控件供给QWidget用。
QWidget嵌入Qml的几种方式
- Qt4的QDeclarativeView
自己也没怎么接触过Qt4,这种方式不做评价,大概率也被Qt5的方式逐渐淘汰了
Qt4的QDeclarativeView *view = new QDeclarativeView;
view->setSource(QUrl(QStringLiteral("qrc:/test.qml"));
view->show();
- QWidget::createWindowContainer结合QQuickView
这种方式就是把加载Qml的window,通过createWindowContainer转换成一个QWidget,而QWidget作为外层widget的子控件;QQuickView创建出来的window作为加载qml的窗口,这个window作为createWindowContainer转换出来的小部件。
这种方式比较绕,并且使用过多的window会拖累程序的性能
QuickView *qml_view = new QQuickView();
qml_view->setResizeMode(QQuickView::SizeRootObjectToView);
qml_view->setSource(QUrl("test.qml"));
QWidget *widget_qml_view= QWidget::createWindowContainer(qml_view, nullptr);
ui.verticalLayout->addWidget(widget_qml_view);
qml_view->show();
- QQuickWidget
这种方式就很像我们在代码中动态创建QWidget小部件了。很方便也很容易懂。这也是官方推荐的一种方式。
QQuickWidget *qml_widget = new QQuickWidget(ui.widget_qml);
qml_widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
qml_widget->setSource(QUrl("test.qml"));
qml_widget->show();
需要注意的点
- QuickView和QQuickWidget加载qml文件的小部件,根结点不能为Window,推荐为Item
- 避免使用QQuickWidget时调用winId(),可能会导致渲染问题
- 设置setResizeMode(SizeRootObjectToView)可以让qml控件跟随外层widget的大小进行变化
最后源码
- qml文件
import QtQuick 2.3
Rectangle {
color: "gray"
radius:10
Image {
id:background_image
anchors.fill: parent
fillMode: Image.PreserveAspectCrop
source: "http://cube.elemecdn.com/6/94/4d3ea53c084bad6931a56d5158a48jpeg.jpeg"
antialiasing: true
}
}
- cpp源码
#include "QtUIDemo.h"
#include <QQuickWidget>
#include <QQmlApplicationEngine>
#include <QQuickView>
QtUIDemo::QtUIDemo(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
//方式一
/* QQuickView *qml_view = new QQuickView(); qml_view->setResizeMode(QQuickView::SizeRootObjectToView); qml_view->setSource(QUrl("test_view.qml")); QWidget *widget = QWidget::createWindowContainer(qml_view, nullptr); ui.verticalLayout->addWidget(widget); qml_view->show(); */
//方式二 推荐
QQuickWidget *qml_widget = new QQuickWidget();
qml_widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
qml_widget->setSource(QUrl("test_view.qml"));
qml_widget->show();
ui.verticalLayout->addWidget(qml_widget);
}
边栏推荐
- HotSpot VM
- Mysql数据库慢sql抓取与分析
- 2/12 didn't learn anything
- Record the pit of NETCORE's memory surge
- 如何修改表中的字段约束条件(类型,default, null等)
- Tips for using dm8huge table
- 【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
- Database, relational database and NoSQL non relational database
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
猜你喜欢
Mlapi series - 04 - network variables and network serialization [network synchronization]
Facebook等大厂超十亿用户数据遭泄露,早该关注DID了
Thread sleep, thread sleep application scenarios
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
Database, relational database and NoSQL non relational database
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
记一次excel XXE漏洞
20、 EEPROM memory (AT24C02) (similar to AD)
About some basic DP -- those things about coins (the basic introduction of DP)
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
随机推荐
MySql數據庫root賬戶無法遠程登陸解决辦法
Mlapi series - 04 - network variables and network serialization [network synchronization]
Execution order of scripts bound to game objects
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos
Practical development of member management applet 06 introduction to life cycle function and user-defined method
MySQL master-slave replication
[FPGA tutorial case 12] design and implementation of complex multiplier based on vivado core
Chinese brand hybrid technology: there is no best technical route, only better products
[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
Record the pit of NETCORE's memory surge
User datagram protocol UDP
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
MySQL about self growth
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
Conditionally [jsonignore]
10個 Istio 流量管理 最常用的例子,你知道幾個?
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
Security xxE vulnerability recurrence (XXe Lab)
Class A, B, C networks and subnet masks in IPv4