当前位置:网站首页>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);
}
边栏推荐
- C mouse event and keyboard event of C (XXVIII)
- Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Record the pit of NETCORE's memory surge
- asp. Core is compatible with both JWT authentication and cookies authentication
- Le compte racine de la base de données MySQL ne peut pas se connecter à distance à la solution
- Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- Brief tutorial for soft exam system architecture designer | general catalog
- MySQL about self growth
猜你喜欢
Proof of Stirling formula
Mlapi series - 04 - network variables and network serialization [network synchronization]
颠覆你的认知?get和post请求的本质
MySQL learning record 13 database connection pool, pooling technology, DBCP, c3p0
【按鍵消抖】基於FPGA的按鍵消抖模塊開發
【可调延时网络】基于FPGA的可调延时网络系统verilog开发
Overturn your cognition? The nature of get and post requests
【FPGA教程案例11】基于vivado核的除法器设计与实现
Execution order of scripts bound to game objects
How many of the 10 most common examples of istio traffic management do you know?
随机推荐
How does technology have the ability to solve problems perfectly
Ks003 mall system based on JSP and Servlet
About some basic DP -- those things about coins (the basic introduction of DP)
解决“C2001:常量中有换行符“编译问题
One question per day (Mathematics)
综合能力测评系统
使用JS完成一个LRU缓存
Esp32 (based on Arduino) connects the mqtt server of emqx to upload information and command control
【leetcode】22. bracket-generating
Record an excel xxE vulnerability
Mysql数据库慢sql抓取与分析
Database, relational database and NoSQL non relational database
HotSpot VM
Understanding of processes, threads, coroutines, synchronization, asynchrony, blocking, non blocking, concurrency, parallelism, and serialization
51nod 1130 n factorial length V2 (Stirling approximation)
Ks008 SSM based press release system
Tips for using dm8huge table
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
Lora gateway Ethernet transmission
In depth MySQL transactions, stored procedures and triggers