当前位置:网站首页>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);
}
边栏推荐
- Brief tutorial for soft exam system architecture designer | general catalog
- 20、 EEPROM memory (AT24C02) (similar to AD)
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- 【按键消抖】基于FPGA的按键消抖模块开发
- Global and Chinese markets for medical gas manifolds 2022-2028: Research Report on technology, participants, trends, market size and share
- R note prophet
- Slow SQL fetching and analysis of MySQL database
- Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage
- Comprehensive ability evaluation system
- math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
猜你喜欢

How to modify field constraints (type, default, null, etc.) in a table
![Cf464e the classic problem [shortest path, chairman tree]](/img/6b/65b2dc62422a45cc72f287c38dbc58.jpg)
Cf464e the classic problem [shortest path, chairman tree]

Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did

图应用详解

Solution to the problem that the root account of MySQL database cannot be logged in remotely

Mysql数据库慢sql抓取与分析

Comprehensive ability evaluation system

关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解

简易博客系统

Ks003 mall system based on JSP and Servlet
随机推荐
Conditionally [jsonignore]
/usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
1291_Xshell日志中增加时间戳的功能
Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
Prime Protocol宣布在Moonbeam上的跨链互连应用程序
User datagram protocol UDP
About some basic DP -- those things about coins (the basic introduction of DP)
Interface idempotency
Error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: no/yes
Execution order of scripts bound to game objects
2/13 review Backpack + monotonic queue variant
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
Cross domain and jsonp details
HotSpot VM
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
asp. Core is compatible with both JWT authentication and cookies authentication
Stack and queue
Basic use of MySQL (it is recommended to read and recite the content)
What is the difference between gateway address and IP address in tcp/ip protocol?
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)