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
qqmlwatcher.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
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 "qqmlwatcher.h"
5
6#include "qqmlexpression.h"
7#include "qqmlcontext.h"
8#include "qqml.h"
9
10#include <private/qqmldebugservice_p.h>
11#include <private/qqmlproperty_p.h>
12#include <private/qqmlvaluetype_p.h>
13
14#include <QtCore/qmetaobject.h>
15#include <QtCore/qdebug.h>
16
18
19
20class QQmlWatchProxy : public QObject
21{
23public:
24 QQmlWatchProxy(int id,
25 QObject *object,
26 int debugId,
27 const QMetaProperty &prop,
28 QQmlWatcher *parent = nullptr);
29
30 QQmlWatchProxy(int id,
31 QQmlExpression *exp,
32 int debugId,
33 QQmlWatcher *parent = nullptr);
34
35public slots:
36 void notifyValueChanged(); // Needs to be a slot because of QQmlPropertyPrivate::connect()
37
38private:
39 friend class QQmlWatcher;
40 int m_id;
41 QQmlWatcher *m_watch;
42 QObject *m_object;
43 int m_debugId;
44 QMetaProperty m_property;
45
46 QQmlExpression *m_expr;
47};
48
50 : QObject(parent),
51 m_id(id),
52 m_watch(parent),
53 m_object(nullptr),
54 m_debugId(debugId),
55 m_expr(exp)
56{
59}
60
61QQmlWatchProxy::QQmlWatchProxy(int id, QObject *object, int debugId, const QMetaProperty &prop,
63 : QObject(parent),
64 m_id(id),
65 m_watch(parent),
66 m_object(object),
67 m_debugId(debugId),
68 m_property(prop),
69 m_expr(nullptr)
70{
71 static int refreshIdx = -1;
72 if(refreshIdx == -1)
73 refreshIdx = QQmlWatchProxy::staticMetaObject.indexOfMethod("notifyValueChanged()");
74
75 if (prop.hasNotifySignal())
76 QQmlPropertyPrivate::connect(m_object, prop.notifySignalIndex(), this, refreshIdx);
77}
78
80{
81 QVariant v;
82 if (m_expr)
83 v = m_expr->evaluate();
84 else
85 v = m_property.read(m_object);
86 emit m_watch->propertyChanged(m_id, m_debugId, m_property, v);
87}
88
89
94
95bool QQmlWatcher::addWatch(int id, quint32 debugId)
96{
97 QObject *object = QQmlDebugService::objectForId(debugId);
98 if (object) {
99 int propCount = object->metaObject()->propertyCount();
100 for (int ii=0; ii<propCount; ii++)
101 addPropertyWatch(id, object, debugId, object->metaObject()->property(ii));
102 return true;
103 }
104 return false;
105}
106
108{
109 QObject *object = QQmlDebugService::objectForId(debugId);
110 if (object) {
111 int index = object->metaObject()->indexOfProperty(property.constData());
112 if (index >= 0) {
113 addPropertyWatch(id, object, debugId, object->metaObject()->property(index));
114 return true;
115 }
116 }
117 return false;
118}
119
120bool QQmlWatcher::addWatch(int id, quint32 objectId, const QString &expr)
121{
122 QObject *object = QQmlDebugService::objectForId(objectId);
123 QQmlContext *context = qmlContext(object);
124 if (context) {
125 QQmlExpression *exprObj = new QQmlExpression(context, object, expr);
126 exprObj->setNotifyOnValueChanged(true);
127 QQmlWatchProxy *proxy = new QQmlWatchProxy(id, exprObj, objectId, this);
128 exprObj->setParent(proxy);
129 m_proxies[id].append(proxy);
130 proxy->notifyValueChanged();
131 return true;
132 }
133 return false;
134}
135
137{
138 if (!m_proxies.contains(id))
139 return false;
140
141 QList<QPointer<QQmlWatchProxy> > proxies = m_proxies.take(id);
142 qDeleteAll(proxies);
143 return true;
144}
145
146void QQmlWatcher::addPropertyWatch(int id, QObject *object, quint32 debugId, const QMetaProperty &property)
147{
148 QQmlWatchProxy *proxy = new QQmlWatchProxy(id, object, debugId, property, this);
149 m_proxies[id].append(proxy);
150
151 proxy->notifyValueChanged();
152}
153
155
156#include <qqmlwatcher.moc>
157
158#include "moc_qqmlwatcher.cpp"
\inmodule QtCore
Definition qbytearray.h:57
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it.
Definition qhash.h:985
bool contains(const Key &key) const noexcept
Returns true if the hash contains an item with the key; otherwise returns false.
Definition qhash.h:1007
\inmodule QtCore
QVariant read(const QObject *obj) const
Reads the property's value from the given object.
int notifySignalIndex() const
bool hasNotifySignal() const
Returns true if this property has a corresponding change notify signal; otherwise returns false.
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
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
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
static QObject * objectForId(int id)
The QQmlExpression class evaluates JavaScript in a QML context.
void valueChanged()
Emitted each time the expression value changes from the last time it was evaluated.
QVariant evaluate(bool *valueIsUndefined=nullptr)
Evaulates the expression, returning the result of the evaluation, or an invalid QVariant if the expre...
static bool connect(const QObject *sender, int signal_index, const QObject *receiver, int method_index, int type=0, int *types=nullptr)
Connect sender signal_index to receiver method_index with the specified type and types.
void notifyValueChanged()
QQmlWatchProxy(int id, QObject *object, int debugId, const QMetaProperty &prop, QQmlWatcher *parent=nullptr)
bool removeWatch(int id)
friend class QQmlWatchProxy
Definition qqmlwatcher.h:48
bool addWatch(int id, quint32 objectId)
void propertyChanged(int id, int objectId, const QMetaProperty &property, const QVariant &value)
QQmlWatcher(QObject *=nullptr)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qvariant.h:65
qDeleteAll(list.begin(), list.end())
Combined button and popup list for selecting options.
static void * context
GLsizei const GLfloat * v
[13]
GLuint index
[2]
GLenum GLuint id
[7]
GLuint object
[3]
GLenum GLuint GLsizei propCount
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:75
#define Q_OBJECT
#define slots
#define emit
unsigned int quint32
Definition qtypes.h:50
const char property[13]
Definition qwizard.cpp:101
QObject::connect nullptr
QNetworkProxy proxy
[0]