Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qqmlapplicationengine.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Research In Motion.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtQml/qqmlfile.h>
5#include <QtCore/QCoreApplication>
6#include <QtCore/QTranslator>
7#include <QQmlComponent>
10#include <QtQml/private/qqmlcomponent_p.h>
11#include <QtQml/private/qqmldirdata_p.h>
12#include <QtQml/private/qqmlfileselector_p.h>
13
15
21
25
33
35{
37 for (auto obj : std::as_const(objects))
38 obj->disconnect(q);
39
41}
42
44{
52 });
53#if QT_CONFIG(translation)
54 QTranslator* qtTranslator = new QTranslator(q);
57 else
58 delete qtTranslator;
59#endif
60 auto *selector = new QQmlFileSelector(q,q);
62 QCoreApplication::instance()->setProperty("__qml_using_qqmlapplicationengine", QVariant(true));
63}
64
66{
67#if QT_CONFIG(translation)
70 return;
71
72 auto translator = std::make_unique<QTranslator>();
73 if (!uiLanguage.value().isEmpty()) {
74 QLocale locale(uiLanguage);
75 if (translator->load(locale, QLatin1String("qml"), QLatin1String("_"), translationsDirectory, QLatin1String(".qm"))) {
76 if (activeTranslator)
77 QCoreApplication::removeTranslator(activeTranslator.get());
79 activeTranslator.swap(translator);
80 }
81 } else {
82 activeTranslator.reset();
83 }
84 q->retranslate();
85#endif
86}
87
89{
91
93
94 if (url.scheme() == QLatin1String("file") || url.scheme() == QLatin1String("qrc")) {
96 translationsDirectory = fi.path() + QLatin1String("/i18n");
97 } else {
99 }
100
101 _q_loadTranslations(); //Translations must be loaded before the QML file is
103
104 if (dataFlag)
105 c->setData(data,url);
106 else
107 c->loadUrl(url);
108
110}
111
113{
115
117
119
120 auto *componentPriv = QQmlComponentPrivate::get(c);
121 const auto [status, type] = componentPriv->prepareLoadFromModule(uri, typeName);
122
123 if (type.sourceUrl().isValid()) {
124 const auto qmlDirData = typeLoader.getQmldir(type.sourceUrl());
125 const QUrl url = qmlDirData->finalUrl();
126 if (url.scheme() == QLatin1String("file") || url.scheme() == QLatin1String("qrc")) {
128 translationsDirectory = fi.path() + QLatin1String("/i18n");
129 } else {
131 }
132 }
133
134 /* Translations must be loaded before the QML file. They require translationDirectory to
135 * already be resolved. But, in order to resolve the translationDirectory, the type of the
136 * module to load needs to be known. Therefore, loadFromModule is split into resolution and
137 * loading because the translation directory needs to be set in between.
138 */
140 componentPriv->completeLoadFromModule(uri, typeName, type, status);
141
143}
144
146{
148 switch (c->status()) {
150 qWarning() << "QQmlApplicationEngine failed to load component";
151 warning(c->errors());
152 q->objectCreated(nullptr, c->url());
153 q->objectCreationFailed(c->url());
154 break;
156 auto newObj = initialProperties.empty() ? c->create() : c->createWithInitialProperties(initialProperties);
157
158 if (c->isError()) {
159 qWarning() << "QQmlApplicationEngine failed to create component";
160 warning(c->errors());
161 q->objectCreated(nullptr, c->url());
162 q->objectCreationFailed(c->url());
163 break;
164 }
165
166 objects << newObj;
168 q->objectCreated(objects.constLast(), c->url());
169 }
170 break;
173 return; //These cases just wait for the next status update
174 }
175
176 c->deleteLater();
177}
178
180{
182 if (!c->isLoading()) {
183 finishLoad(c);
184 return;
185 }
186 QObject::connect(c, &QQmlComponent::statusChanged, q, [this, c] { this->finishLoad(c); });
187}
188
283
293
307
317 : QQmlApplicationEngine(QUrl::fromUserInput(filePath, QLatin1String("."), QUrl::AssumeLocalFile), parent)
318{
319}
320
325{
328 d->cleanUp();//Instantiated root objects must be deleted before the engine
329}
330
342{
344 d->startLoad(url);
345}
346
356{
358 d->startLoad(QUrl::fromUserInput(filePath, QLatin1String("."), QUrl::AssumeLocalFile));
359}
360
390
413{
415 d->initialProperties = initialProperties;
416}
417
428{
430 if (d->isInitialized) {
431 qWarning() << "QQmlApplicationEngine::setExtraFileSelectors()"
432 << "called after loading QML files. This has no effect.";
433 } else {
434 d->extraFileSelectors = extraFileSelectors;
435 }
436}
437
448{
450 d->startLoad(url, data, true);
451}
452
462{
463 Q_D(const QQmlApplicationEngine);
464 return d->objects;
465}
466
468
469#include "moc_qqmlapplicationengine.cpp"
\inmodule QtCore
\inmodule QtCore
Definition qbytearray.h:57
static void quit()
\threadsafe
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
static bool installTranslator(QTranslator *messageFile)
Adds the translation file translationFile to the list of translation files to be used for translation...
static bool removeTranslator(QTranslator *messageFile)
Removes the translation file translationFile from the list of translation files used by this applicat...
static void exit(int retcode=0)
Tells the application to exit with a return code.
void setExtraSelectors(const QStringList &list)
Sets the list of extra selectors which have been added programmatically to this instance.
static void addToDebugServer(QJSEngine *q)
static void removeFromDebugServer(QJSEngine *q)
void uiLanguageChanged()
static QString path(LibraryPath p)
const T & constLast() const noexcept
Definition qlist.h:650
qsizetype removeAll(const AT &t)
Definition qlist.h:592
QString bcp47Name(TagSeparator separator=TagSeparator::Dash) const
Returns the BCP47 field names joined with dashes.
Definition qlocale.cpp:1482
bool empty() const
Definition qmap.h:761
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void ensureLoadingFinishes(QQmlComponent *component)
void finishLoad(QQmlComponent *component)
void startLoad(const QUrl &url, const QByteArray &data=QByteArray(), bool dataFlag=false)
QQmlApplicationEngine provides a convenient way to load an application from a single QML file.
void setInitialProperties(const QVariantMap &initialProperties)
Sets the initialProperties with which the QML component gets initialized after it gets loaded.
void loadFromModule(QAnyStringView uri, QAnyStringView typeName)
Loads the QML type typeName from the module specified by uri.
QList< QObject * > rootObjects() const
Returns a list of all the root objects instantiated by the QQmlApplicationEngine.
~QQmlApplicationEngine() override
Destroys the QQmlApplicationEngine and all QML objects it loaded.
QQmlApplicationEngine(QObject *parent=nullptr)
Create a new QQmlApplicationEngine with the given parent.
void loadData(const QByteArray &data, const QUrl &url=QUrl())
Loads the QML given in data.
void load(const QUrl &url)
Loads the root QML file located at url.
void setExtraFileSelectors(const QStringList &extraFileSelectors)
Sets the extraFileSelectors to be passed to the internal QQmlFileSelector used for resolving URLs to ...
static QQmlComponentPrivate * get(QQmlComponent *c)
The QQmlComponent class encapsulates a QML component definition.
void statusChanged(QQmlComponent::Status)
Emitted whenever the component's status changes.
void warning(const QQmlError &)
QQmlTypeLoader typeLoader
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
void quit()
This signal is emitted when the QML loaded by the engine would like to quit.
void exit(int retCode)
This signal is emitted when the QML loaded by the engine would like to exit from the event loop with ...
A class for applying a QFileSelector to QML file loading.
static QString urlToLocalFileOrQrc(const QString &)
If url is a local file returns a path suitable for passing to \l{QFile}.
Definition qqmlfile.cpp:742
QQmlRefPointer< QQmlQmldirData > getQmldir(const QUrl &)
Returns a QQmlQmldirData for url.
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1252
\inmodule QtCore
Definition qtranslator.h:19
\inmodule QtCore
Definition qurl.h:94
static QUrl fromUserInput(const QString &userInput, const QString &workingDirectory=QString(), UserInputResolutionOptions options=DefaultResolution)
Returns a valid URL from a user supplied userInput string if one can be deduced.
Definition qurl.cpp:3757
@ AssumeLocalFile
Definition qurl.h:175
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1991
\inmodule QtCore
Definition qvariant.h:65
#define this
Definition dialogs.cpp:9
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
@ QueuedConnection
#define qWarning
Definition qlogging.h:166
const char * typeName
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLhandleARB obj
[2]
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QFileSelector selector
[1]
QUrl url("example.com")
[constructor-url-reference]