QQmlComponent
时间: 2023-09-22 13:08:40 浏览: 42
QQmlComponent是Qt框架中的一个类,用于在运行时动态创建QML对象。它允许使用QML语言编写的UI组件在C++代码中进行动态创建和管理。通常,QQmlComponent与QQmlEngine一起使用,QQmlEngine用于解析和管理QML文件,而QQmlComponent用于创建和管理QML组件。QQmlComponent的创建过程包括QML文件的解析、编译和实例化。一旦组件被创建,它就可以像普通的对象一样在C++代码中使用,并且可以通过QQmlEngine与其他QML组件进行交互。QQmlComponent在Qt Quick应用程序中扮演着重要的角色,它允许开发人员在运行时动态地创建和管理QML组件,从而使应用程序更加灵活和可扩展。
相关问题
==9114== HEAP SUMMARY: ==9114== in use at exit: 14,336,980 bytes in 90,048 blocks ==9114== total heap usage: 428,295 allocs, 338,247 frees, 58,358,339 bytes allocated ==9114== ==9114== 0 bytes in 1 blocks are still reachable in loss record 1 of 25,502 ==9114== at 0x4C3289F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==9114== by 0x66F9EBA: QV4::CompiledData::CompilationUnit::linkToEngine(QV4::ExecutionEngine*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x6904054: QQmlObjectCreator::init(QQmlContextData*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x6904202: QQmlObjectCreator::QQmlObjectCreator(QQmlContextData*, QQmlRefPointer<QV4::CompiledData::CompilationUnit> const&, QQmlContextData*, QQmlIncubatorPrivate*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x688E2E1: QQmlComponentPrivate::beginCreate(QQmlContextData*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x688BE48: QQmlComponent::create(QQmlContext*) (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5Qml.so.5.12.9) ==9114== by 0x4E49D6E: QQuickWidget::continueExecute() (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5QuickWidgets.so.5.12.9) ==9114== by 0x4E4A18A: QQuickWidgetPrivate::execute() (in /opt/Qt5.12.9/5.12.9/gcc_64/lib/libQt5QuickWidgets.so.5.12.9) ==9114== by 0x1397D2: TWDZVirtualKeyboard::TWDZVirtualKeyboard() (twdzvirtualkeyboard.cpp:24) ==9114== by 0x13965E: TWDZVirtualKeyboard::GetInstance() (twdzvirtualkeyboard.cpp:8) ==9114== by 0x136629: UartTest::UartTest(int, int, QWidget*) (uarttest.cpp:18) ==9114== by 0x136157: communicationModule::initUi() (communicationmodule.cpp:11)
这是 Valgrind 的 HEAP SUMMARY 部分的输出,它显示程序在退出时堆的使用情况。具体来说,它提供了以下信息:
- "in use at exit":退出时在堆上仍然使用的字节数和块数。
- "total heap usage":程序整个运行期间在堆上分配、释放的字节数和块数。
- "0 bytes in 1 blocks are still reachable":这是一个 loss record,表示在程序退出时,有一个块没有被释放,但是指针仍然可以访问到该块。在这个例子中,这个块是由 `operator new[]` 分配的,由 Qt5Qml 库的 `QQmlObjectCreator::init()` 函数链接到引擎中。这并不一定是一个内存泄漏,因为程序结束时操作系统会自动释放所有未释放的内存。但是,如果这些内存块是在一个长时间运行的程序中分配的,可能会导致内存泄漏。
C++调用main.qml之外qml文件中的函数示例代码
以下是C++调用main.qml之外qml文件中的函数的示例代码:
假设我们有一个名为"OtherQML.qml"的qml文件,其中包含一个名为"otherFunction()"的函数,我们可以通过以下步骤在C++中调用该函数:
1.在C++代码中包含qml文件:
```cpp
#include <QQmlComponent>
#include <QQmlEngine>
#include <QQmlContext>
QQmlComponent component(qmlEngine);
component.loadUrl(QUrl(QStringLiteral("qrc:/OtherQML.qml")));
QObject *object = component.create();
```
2.将qml文件中的函数转换为QMetaObject::invokeMethod()函数的参数:
```cpp
QMetaObject::invokeMethod(object, "otherFunction");
```
完整的示例代码如下:
```cpp
#include <QQmlComponent>
#include <QQmlEngine>
#include <QQmlContext>
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QQmlEngine qmlEngine;
QQmlComponent component(&qmlEngine);
component.loadUrl(QUrl(QStringLiteral("qrc:/OtherQML.qml")));
QObject *object = component.create();
QMetaObject::invokeMethod(object, "otherFunction");
return app.exec();
}
```