当前位置:网站首页>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);
}
边栏推荐
- C mouse event and keyboard event of C (XXVIII)
- lora网关以太网传输
- Prime protocol announces cross chain interconnection applications on moonbeam
- Web components series (VII) -- life cycle of custom components
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- [adjustable delay network] development of FPGA based adjustable delay network system Verilog
- MySql数据库root账户无法远程登陆解决办法
- MySQL reads missing data from a table in a continuous period of time
- Custom event of C (31)
- Basic use of MySQL (it is recommended to read and recite the content)
猜你喜欢
KS003基于JSP和Servlet实现的商城系统
Stable Huawei micro certification, stable Huawei cloud database service practice
After five years of testing in byte, I was ruthlessly dismissed in July, hoping to wake up my brother who was paddling
Database, relational database and NoSQL non relational database
Facebook等大厂超十亿用户数据遭泄露,早该关注DID了
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
C mouse event and keyboard event of C (XXVIII)
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
C language -- structs, unions, enumerations, and custom types
About some basic DP -- those things about coins (the basic introduction of DP)
随机推荐
[disassembly] a visual air fryer. By the way, analyze the internal circuit
C form application of C (27)
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
2/10 parallel search set +bfs+dfs+ shortest path +spfa queue optimization
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
KS008基于SSM的新闻发布系统
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
MySQL about self growth
Class A, B, C networks and subnet masks in IPv4
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
Prime Protocol宣布在Moonbeam上的跨链互连应用程序
asp. Core is compatible with both JWT authentication and cookies authentication
综合能力测评系统
1291_Xshell日志中增加时间戳的功能
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
Stable Huawei micro certification, stable Huawei cloud database service practice
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share