当前位置:网站首页>Mixed development of QML and QWidget (preliminary exploration)
Mixed development of QML and QWidget (preliminary exploration)
2022-07-06 04:12:00 【BUG_ C++】
Why should we engage in mixed development ?
Qml Has become more and more Qt Mainstream of development , Compared with the QWidget Faster interface development , It's easier to get started , The implementation effect is also better . But old projects are QWidget Framework , It is impossible for everyone to put QWidget Change the project interface to qml, At this time, we can use the newly developed interface qml. It can also be used. qml Do some control supply QWidget use .
QWidget The embedded Qml In several ways
- Qt4 Of QDeclarativeView
I haven't had much contact with Qt4, In this way, there is no evaluation , Probability is also Qt5 The way of
Qt4 Of QDeclarativeView *view = new QDeclarativeView;
view->setSource(QUrl(QStringLiteral("qrc:/test.qml"));
view->show();
- QWidget::createWindowContainer combination QQuickView
This way is to load Qml Of window, adopt createWindowContainer Convert to a QWidget, and QWidget As an outer layer widget Child controls ;QQuickView created window As the load qml The window of , This window As createWindowContainer Converted widget .
This way is more around , And use too much window It will drag down the performance of the program
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
This method is very similar to our dynamic creation in code QWidget Widget . It's very convenient and easy to understand . It's also an official way of recommending .
QQuickWidget *qml_widget = new QQuickWidget(ui.widget_qml);
qml_widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
qml_widget->setSource(QUrl("test.qml"));
qml_widget->show();
Something to watch out for
- QuickView and QQuickWidget load qml File widget , The root node cannot be Window, Recommended as Item
- Avoid using QQuickWidget Called when the winId(), May cause rendering problems
- Set up setResizeMode(SizeRootObjectToView) It can make qml Control follows the outer layer widget Change the size of
Final source code
- qml file
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 Source code
#include "QtUIDemo.h"
#include <QQuickWidget>
#include <QQmlApplicationEngine>
#include <QQuickView>
QtUIDemo::QtUIDemo(QWidget *parent) : QWidget(parent)
{
ui.setupUi(this);
// Mode one
/* 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(); */
// Mode two recommend
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);
}
边栏推荐
- 1291_Xshell日志中增加时间戳的功能
- 2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
- In depth MySQL transactions, stored procedures and triggers
- Class A, B, C networks and subnet masks in IPv4
- Lora gateway Ethernet transmission
- TCP/IP协议里面的网关地址和ip地址有什么区别?
- Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
- Script lifecycle
- Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Several important classes in unity
猜你喜欢
Custom event of C (31)
[adjustable delay network] development of FPGA based adjustable delay network system Verilog
Detailed explanation of serialization and deserialization
Practical development of member management applet 06 introduction to life cycle function and user-defined method
When debugging after pycharm remote server is connected, trying to add breakpoint to file that does not exist: /data appears_ sda/d:/segmentation
In Net 6 CS more concise method
Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
TCP/IP协议里面的网关地址和ip地址有什么区别?
10个 Istio 流量管理 最常用的例子,你知道几个?
Mlapi series - 04 - network variables and network serialization [network synchronization]
随机推荐
Python book learning notes - Chapter 09 section 01 create and use classes
P2022 有趣的数(二分&数位dp)
使用JS完成一个LRU缓存
2/13 review Backpack + monotonic queue variant
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
Overturn your cognition? The nature of get and post requests
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
[tomato assistant installation]
Chinese brand hybrid technology: there is no best technical route, only better products
【leetcode】22. bracket-generating
[disassembly] a visual air fryer. By the way, analyze the internal circuit
Solution of storage bar code management system in food industry
Ipv4中的A 、B、C类网络及子网掩码
10个 Istio 流量管理 最常用的例子,你知道几个?
729. 我的日程安排表 I(set or 动态开点线段树)
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
Database, relational database and NoSQL non relational database
Ks008 SSM based press release system
Lombok原理和同时使⽤@Data和@Builder 的坑
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】