QQmlApplicationEngine 提供從單個 QML 文件加載應用程序的便捷方式。 更多...
| 頭: |
#include <QQmlApplicationEngine>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Qml)
target_link_libraries(mytarget PRIVATE Qt6::Qml)
|
| qmake: |
QT += qml
|
| 繼承: | QQmlEngine |
| QQmlApplicationEngine (QObject * parent = nullptr) | |
| QQmlApplicationEngine (const QString & filePath , QObject * parent = nullptr) | |
| QQmlApplicationEngine (const QUrl & url , QObject * parent = nullptr) | |
(從 6.5 起)
|
QQmlApplicationEngine (QAnyStringView uri , QAnyStringView typeName , QObject * parent = nullptr) |
| virtual | ~QQmlApplicationEngine () override |
| QList<QObject *> | rootObjects () const |
| void | load (const QString & filePath ) |
| void | load (const QUrl & url ) |
| void | loadData (const QByteArray & data , const QUrl & url = QUrl()) |
(從 6.5 起)
void
|
loadFromModule (QAnyStringView uri , QAnyStringView typeName ) |
(從 6.0 起)
void
|
setExtraFileSelectors (const QStringList & extraFileSelectors ) |
| void | setInitialProperties (const QVariantMap & initialProperties ) |
| void | objectCreated (QObject * object , const QUrl & url ) |
(從 6.4 起)
void
|
objectCreationFailed (const QUrl & url ) |
此類組閤 QQmlEngine and QQmlComponent 提供加載單個 QML 文件的便捷方式。它還嚮 QML 暴露某些中心應用程序功能,C++/QML 混閤應用程序通常從 C++ 進行控製。
它可以像這樣使用:
#include <QGuiApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine("main.qml"); return app.exec(); }
不像 QQuickView ,QQmlApplicationEngine 不會自動創建根窗口。若正使用來自 Qt Quick 的視覺項,需要將它們放入 Window .
還可以使用
QCoreApplication
采用 QQmlApplicationEngine,若未使用任何 QML 模塊要求
QGuiApplication
(譬如
QtQuick
).
配置列錶的改變從默認 QQmlEngine :
QJSEngine::uiLanguage
/
Qt.uiLanguage
特性改變。
QQmlFileSelector
作為 URL 攔截器,將文件選擇器應用於所有 QML 文件和資産。
可以進一步微調引擎行為,通過使用繼承的方法從 QQmlEngine .
注意: Translation files must have a qml_ prefix in order to be recognized, e.g. qml_ja_JP.qm .
注意: Placing translation files relative to the main QML file involves adding a RESOURCE_PREFIX to the relevant qt_add_translations call. This needs to include the resource prefix of the main file's QML module ( /qt/qml by default) and the module URI. For example, to provide translation files for a module called "Translated":
qt_add_translations(App
RESOURCE_PREFIX /qt/qml/Translated/i18n
TS_FILE_BASE qml
TS_FILE_DIR i18n
)
Create a new QQmlApplicationEngine with the given parent . You will have to call load () later in order to load a QML file.
Create a new QQmlApplicationEngine and loads the QML file at the given filePath , which must be a local file or qrc path. If a relative path is given then it will be interpreted as relative to the working directory of the application.
This is provided as a convenience, and is the same as using the empty constructor and calling load afterwards.
Create a new QQmlApplicationEngine and loads the QML file at the given url . This is provided as a convenience, and is the same as using the empty constructor and calling load afterwards.
[explicit, since 6.5]
QQmlApplicationEngine::
QQmlApplicationEngine
(
QAnyStringView
uri
,
QAnyStringView
typeName
,
QObject
*
parent
= nullptr)
Create a new QQmlApplicationEngine and loads the QML type specified by uri and typeName This is provided as a convenience, and is the same as using the empty constructor and calling loadFromModule afterwards.
該函數在 Qt 6.5 引入。
[override virtual noexcept]
QQmlApplicationEngine::
~QQmlApplicationEngine
()
銷毀 QQmlApplicationEngine and all QML objects it loaded.
[slot]
void
QQmlApplicationEngine::
load
(const
QString
&
filePath
)
Loads the root QML file located at filePath . filePath must be a path to a local file or a path to a file in the resource file system. If filePath is a relative path, it is taken as relative to the application's working directory. The object tree defined by the file is instantiated immediately.
若齣現錯誤,打印錯誤消息采用 qWarning .
注意: 此槽被重載。要連接到此槽:
// Connect using qOverload: connect(qmlApplicationEngine, qOverload(&QQmlApplicationEngine::load), receiver, &ReceiverClass::slot); // Or using a lambda: connect(qmlApplicationEngine, qOverload (&QQmlApplicationEngine::load), this, [](const QString &filePath) { /* handle load */ });
更多範例和方式,見 連接到重載槽 .
[slot]
void
QQmlApplicationEngine::
load
(const
QUrl
&
url
)
Loads the root QML file located at url . The object tree defined by the file is created immediately for local file urls. Remote urls are loaded asynchronously, listen to the objectCreated signal to determine when the object tree is ready.
If an error occurs, the objectCreated signal is emitted with a null pointer as parameter and error messages are printed with qWarning .
注意: 此槽被重載。要連接到此槽:
// Connect using qOverload: connect(qmlApplicationEngine, qOverload(&QQmlApplicationEngine::load), receiver, &ReceiverClass::slot); // Or using a lambda: connect(qmlApplicationEngine, qOverload (&QQmlApplicationEngine::load), this, [](const QUrl &url) { /* handle load */ });
更多範例和方式,見 連接到重載槽 .
[slot]
void
QQmlApplicationEngine::
loadData
(const
QByteArray
&
data
, const
QUrl
&
url
= QUrl())
Loads the QML given in data . The object tree defined by data is instantiated immediately.
若 url is specified it is used as the base url of the component. This affects relative paths within the data and error messages.
若齣現錯誤,打印錯誤消息采用 qWarning .
[slot, since 6.5]
void
QQmlApplicationEngine::
loadFromModule
(
QAnyStringView
uri
,
QAnyStringView
typeName
)
Loads the QML type typeName from the module specified by uri . If the type originates from a QML file located at a remote url, the type will be loaded asynchronously. Listen to the objectCreated signal to determine when the object tree is ready.
If an error occurs, the objectCreated signal is emitted with a null pointer as parameter and error messages are printed with qWarning .
QQmlApplicationEngine engine; engine.loadFromModule("QtQuick", "Rectangle");
注意:
The module identified by
uri
is searched in the
import 路徑
, in the same way as if you were doing
import uri
inside a QML file. If the module cannot be located there, this function will fail.
該函數在 Qt 6.5 引入。
另請參閱 QQmlComponent::loadFromModule .
[signal]
void
QQmlApplicationEngine::
objectCreated
(
QObject
*
object
, const
QUrl
&
url
)
This signal is emitted when an object finishes loading. If loading was successful, object contains a pointer to the loaded object, otherwise the pointer is NULL.
The url to the component the object came from is also provided.
注意: If the path to the component was provided as a QString containing a relative path, the url will contain a fully resolved path to the file.
[signal, since 6.4]
void
QQmlApplicationEngine::
objectCreationFailed
(const
QUrl
&
url
)
This signal is emitted when loading finishes because an error occurred.
The url to the component that failed to load is provided as an argument.
QGuiApplication app(argc, argv); QQmlApplicationEngine engine; // exit on error QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); engine.load(QUrl()); return app.exec();
注意: If the path to the component was provided as a QString containing a relative path, the url will contain a fully resolved path to the file.
另請參閱 QQmlApplicationEngine::objectCreated , which will be emitted in addition to this signal (even though creation failed).
該函數在 Qt 6.4 引入。
Returns a list of all the root objects instantiated by the QQmlApplicationEngine . This will only contain objects loaded via load () or a convenience constructor.
注意:
In Qt versions prior to 5.9, this function is marked as non-
const
.
[slot, since 6.0]
void
QQmlApplicationEngine::
setExtraFileSelectors
(const
QStringList
&
extraFileSelectors
)
設置 extraFileSelectors to be passed to the internal QQmlFileSelector used for resolving URLs to local files. The extraFileSelectors are applied when the first QML file is loaded. Setting them afterwards has no effect.
該函數在 Qt 6.0 引入。
另請參閱 QQmlFileSelector and QFileSelector::setExtraSelectors .
[slot]
void
QQmlApplicationEngine::
setInitialProperties
(const
QVariantMap
&
initialProperties
)
設置 initialProperties with which the QML component gets initialized after it gets loaded.
QQmlApplicationEngine engine; EventDatabase eventDatabase; EventMonitor eventMonitor; engine.setInitialProperties({ { "eventDatabase", QVariant::fromValue(&eventDatabase) }, { "eventMonitor", QVariant::fromValue(&eventMonitor) } });
另請參閱 QQmlComponent::setInitialProperties , QQmlApplicationEngine::load ,和 QQmlApplicationEngine::loadData .