当前位置:网站首页>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);
}
边栏推荐
- Stable Huawei micro certification, stable Huawei cloud database service practice
- MySQL master-slave replication
- MySQL transaction isolation level
- C form application of C (27)
- Lora gateway Ethernet transmission
- Python book learning notes - Chapter 09 section 01 create and use classes
- Stack and queue
- DM8 backup set deletion
- 判断当天是当月的第几周
- Yyds dry goods inventory web components series (VII) -- life cycle of custom components
猜你喜欢
Lora gateway Ethernet transmission
AcWing 243. A simple integer problem 2 (tree array interval modification interval query)
MySQL about self growth
20、 EEPROM memory (AT24C02) (similar to AD)
C (XXIX) C listbox CheckedListBox Imagelist
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Cross domain and jsonp details
【FPGA教程案例11】基于vivado核的除法器设计与实现
ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
Solution of storage bar code management system in food industry
随机推荐
STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
PTA tiantisai l1-078 teacher Ji's return (15 points) detailed explanation
【FPGA教程案例11】基于vivado核的除法器设计与实现
hashlimit速率控制
查询mysql数据库中各表记录数大小
[FPGA tutorial case 12] design and implementation of complex multiplier based on vivado core
Stack and queue
Cross domain and jsonp details
C. The Third Problem(找规律)
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
[FPGA tutorial case 11] design and implementation of divider based on vivado core
Query the number and size of records in each table in MySQL database
Determine which week of the month the day is
[tomato assistant installation]
Mysql数据库慢sql抓取与分析
How to execute an SQL statement in MySQL
Pandora IOT development board learning (HAL Library) - Experiment 9 PWM output experiment (learning notes)
脚本生命周期
Recommendation system (IX) PNN model (product based neural networks)