当前位置:网站首页>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);
}
边栏推荐
- Solution of storage bar code management system in food industry
- Conditionally [jsonignore]
- Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)
- 【按键消抖】基于FPGA的按键消抖模块开发
- Tips for using dm8huge table
- Stable Huawei micro certification, stable Huawei cloud database service practice
- Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
- Overturn your cognition? The nature of get and post requests
- 【可调延时网络】基于FPGA的可调延时网络系统verilog开发
- Global and Chinese market of plasma separator 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Data processing methods - smote series and adasyn
Thread sleep, thread sleep application scenarios
绑定在游戏对象上的脚本的执行顺序
Développement d'un module d'élimination des bavardages à clé basé sur la FPGA
Comprehensive ability evaluation system
Mlapi series - 04 - network variables and network serialization [network synchronization]
The Research Report "2022 RPA supplier strength matrix analysis of China's banking industry" was officially launched
[FPGA tutorial case 11] design and implementation of divider based on vivado core
IDEA编译JSP页面生成的class文件路径
《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动
随机推荐
Stack and queue
WPF effect Article 191 box selection listbox
判断当天是当月的第几周
MySql數據庫root賬戶無法遠程登陸解决辦法
During pycharm debugging, the view is read only and pause the process to use the command line appear on the console input
Conditionally [jsonignore]
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
Solution to the problem that the root account of MySQL database cannot be logged in remotely
Unity中几个重要类
E. Best Pair
Lombok原理和同时使⽤@Data和@Builder 的坑
Ks008 SSM based press release system
C (XXIX) C listbox CheckedListBox Imagelist
729. 我的日程安排表 I(set or 动态开点线段树)
DM8 archive log file manual switching
Tips for using dm8huge table
Cross domain and jsonp details
[FPGA tutorial case 11] design and implementation of divider based on vivado core
使用JS完成一个LRU缓存
[adjustable delay network] development of FPGA based adjustable delay network system Verilog