Qt 6.x
The Qt SDK
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/qqmlfileselector_p.h>
11
12#include <memory>
13
15
18{
19 uiLanguage = QLocale().bcp47Name();
20}
21
23{
24}
25
27{
28 if (!isInitialized) {
29 init();
30 isInitialized = true;
31 }
32}
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")) {
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
116 _q_loadTranslations(); //Translations must be loaded before the QML file is
118
120 c->loadFromModule(uri, type);
122}
123
125{
127 switch (c->status()) {
129 qWarning() << "QQmlApplicationEngine failed to load component";
130 warning(c->errors());
131 q->objectCreated(nullptr, c->url());
132 q->objectCreationFailed(c->url());
133 break;
135 auto newObj = initialProperties.empty() ? c->create() : c->createWithInitialProperties(initialProperties);
136
137 if (c->isError()) {
138 qWarning() << "QQmlApplicationEngine failed to create component";
139 warning(c->errors());
140 q->objectCreated(nullptr, c->url());
141 q->objectCreationFailed(c->url());
142 break;
143 }
144
145 objects << newObj;
147 q->objectCreated(objects.constLast(), c->url());
148 }
149 break;
152 return; //These cases just wait for the next status update
153 }
154
155 c->deleteLater();
156}
157
159{
161 if (!c->isLoading()) {
162 finishLoad(c);
163 return;
164 }
165 QObject::connect(c, &QQmlComponent::statusChanged, q, [this, c] { this->finishLoad(c); });
166}
167
260{
262}
263
270{
271 load(url);
272}
273
284{
286}
287
297 : QQmlApplicationEngine(QUrl::fromUserInput(filePath, QLatin1String("."), QUrl::AssumeLocalFile), parent)
298{
299}
300
305{
308 d->cleanUp();//Instantiated root objects must be deleted before the engine
309}
310
322{
324 d->startLoad(url);
325}
326
336{
338 d->startLoad(QUrl::fromUserInput(filePath, QLatin1String("."), QUrl::AssumeLocalFile));
339}
340
361{
363 d->startLoad(uri, typeName);
364}
365
388{
390 d->initialProperties = initialProperties;
391}
392
403{
405 if (d->isInitialized) {
406 qWarning() << "QQmlApplicationEngine::setExtraFileSelectors()"
407 << "called after loading QML files. This has no effect.";
408 } else {
409 d->extraFileSelectors = extraFileSelectors;
410 }
411}
412
423{
425 d->startLoad(url, data, true);
426}
427
437{
438 Q_D(const QQmlApplicationEngine);
439 return d->objects;
440}
441
443
444#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.
\inmodule QtCore \reentrant
Definition qfileinfo.h:22
QString path() const
Returns the file's path.
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)
Definition qlist.h:74
const T & constLast() const noexcept
Definition qlist.h:633
qsizetype removeAll(const AT &t)
Definition qlist.h:575
QString bcp47Name() const
Returns the BCP47 field names joined with dashes.
Definition qlocale.cpp:1397
bool empty() const
Definition qmap.h:760
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object's name property to value.
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 ...
The QQmlComponent class encapsulates a QML component definition.
void statusChanged(QQmlComponent::Status)
Emitted whenever the component's status changes.
void warning(const QQmlError &)
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 QFile.
Definition qqmlfile.cpp:643
\inmodule QtCore
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
void clear()
Clears the contents of the string and makes it null.
Definition qstring.h:1107
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
\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:3743
@ AssumeLocalFile
Definition qurl.h:175
QString scheme() const
Returns the scheme of the URL.
Definition qurl.cpp:1983
\inmodule QtCore
Definition qvariant.h:64
#define this
Definition dialogs.cpp:9
qDeleteAll(list.begin(), list.end())
double e
Combined button and popup list for selecting options.
@ QueuedConnection
#define qWarning
Definition qlogging.h:162
const char * typeName
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLhandleARB obj
[2]
const GLubyte * c
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
QFileInfo fi("c:/temp/foo")
[newstuff]
QFileSelector selector
[1]
QUrl url("example.com")
[constructor-url-reference]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent