-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickwidget.cpp
50 lines (43 loc) · 1.38 KB
/
quickwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "quickwidget.h"
#include <QFileInfo>
#include <QHash>
#include <QShortcut>
#include <QString>
#include <QtQml/QQmlComponent>
#include <QtQml/QQmlContext>
#include <QtQml/QQmlEngine>
#include <QtQuick/QQuickItem>
QuickWidget::QuickWidget(QWidget * parent)
: QQuickWidget(parent)
{
setResizeMode(SizeRootObjectToView);
#if !defined(NDEBUG)
QShortcut * shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_R), this);
QObject::connect(shortcut, &QShortcut::activated, [=]() {
const QUrl url = QUrl::fromLocalFile(QString(QML_SRCDIR "%1").arg(qmlFileName_));
const QFileInfo fileInfo(url.toLocalFile());
if (fileInfo.isReadable()) {
qInfo("reloading %s", qUtf8Printable(fileInfo.fileName()));
engine()->clearComponentCache();
setSource(url);
}
});
#endif
}
void QuickWidget::setContextProperties(QHash<QString, QObject *> && contextProperties) {
QHash<QString, QObject *>::const_iterator i = contextProperties.constBegin();
while (i != contextProperties.constEnd()) {
rootContext()->setContextProperty(i.key(), i.value());
++i;
}
}
void QuickWidget::initQmlScene(const QUrl & url)
{
setSource(url);
foreach (const QQmlError & error, errors()) {
qWarning("Error in file %s in line %d: %s", qUtf8Printable(url.toString()), error.line(), qUtf8Printable(error.toString()));
}
#if !defined(NDEBUG)
qmlFileName_ = url.toString().replace("qrc:", "");
#endif
}