Skip to content

Commit 8498ab2

Browse files
author
hekra
committed
add findChild for QML JS
1 parent fb8ed3d commit 8498ab2

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

Diff for: src/webdriver/extension_qt/qml_objname_util.cc

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/****************************************************************************
2+
**
3+
** Copyright © 1992-2014 Cisco and/or its affiliates. All rights reserved.
4+
** All rights reserved.
5+
**
6+
** $CISCO_BEGIN_LICENSE:LGPL$
7+
**
8+
** GNU Lesser General Public License Usage
9+
** Alternatively, this file may be used under the terms of the GNU Lesser
10+
** General Public License version 2.1 as published by the Free Software
11+
** Foundation and appearing in the file LICENSE.LGPL included in the
12+
** packaging of this file. Please review the following information to
13+
** ensure the GNU Lesser General Public License version 2.1 requirements
14+
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
15+
**
16+
** $CISCO_END_LICENSE$
17+
**
18+
****************************************************************************/
19+
20+
#include "qml_objname_util.h"
21+
#include "webdriver_util.h"
22+
23+
24+
namespace webdriver {
25+
26+
ObjectNameUtils::ObjectNameUtils(QQuickItem *root) : root_(root) {}
27+
ObjectNameUtils::~ObjectNameUtils() {}
28+
29+
QObject *ObjectNameUtils::findChild(const QString& name) {
30+
QObject *obj = findChild(root_, name);
31+
return obj;
32+
}
33+
34+
} // namespace webdriver

Diff for: src/webdriver/extension_qt/qml_objname_util.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/****************************************************************************
2+
**
3+
** Copyright © 1992-2014 Cisco and/or its affiliates. All rights reserved.
4+
** All rights reserved.
5+
**
6+
** $CISCO_BEGIN_LICENSE:LGPL$
7+
**
8+
** GNU Lesser General Public License Usage
9+
** Alternatively, this file may be used under the terms of the GNU Lesser
10+
** General Public License version 2.1 as published by the Free Software
11+
** Foundation and appearing in the file LICENSE.LGPL included in the
12+
** packaging of this file. Please review the following information to
13+
** ensure the GNU Lesser General Public License version 2.1 requirements
14+
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
15+
**
16+
** $CISCO_END_LICENSE$
17+
**
18+
****************************************************************************/
19+
20+
#ifndef WEBDRIVER_QT_QML_OBJN_UTIL_H_
21+
#define WEBDRIVER_QT_QML_OBJN_UTIL_H_
22+
23+
#include <string>
24+
#include <QtCore/QDebug>
25+
#include <QtQuick/QQuickItem>
26+
#include "common_util.h"
27+
28+
namespace webdriver {
29+
30+
class ObjectNameUtils
31+
: public QObject
32+
{
33+
Q_OBJECT
34+
public:
35+
ObjectNameUtils(QQuickItem *root);
36+
~ObjectNameUtils();
37+
Q_INVOKABLE QObject *findChild(const QString& name);
38+
protected:
39+
QQuickItem* root_;
40+
41+
QObject * findChild(const QQuickItem* item, const QString& name) {
42+
QList<QQuickItem*> childs = item->childItems();
43+
44+
foreach(QQuickItem *child, childs) {
45+
if (!child) continue;
46+
if (name == child->objectName())
47+
return child;
48+
49+
QObject* q = findChild(child, name);
50+
51+
if (q)
52+
return q;
53+
}
54+
55+
return NULL;
56+
}
57+
58+
};
59+
} // namespace webdriver
60+
61+
#endif // WEBDRIVER_QT_QML_OBJN_UTIL_H_

Diff for: src/webdriver/extension_qt/quick2_view_executor.cc

+12-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "webdriver_util.h"
3030
#include "q_key_converter.h"
3131
#include "qml_view_util.h"
32+
#include "qml_objname_util.h"
3233

3334
#include "extension_qt/event_dispatcher.h"
3435
#include "extension_qt/wd_event_dispatcher.h"
@@ -40,6 +41,7 @@
4041
#include <QtCore/QDebug>
4142
#include <QtCore/QTimer>
4243
#include <QtGui/QGuiApplication>
44+
#include <QtQml/QQmlContext>
4345
#include <QtQml/QQmlExpression>
4446
#include <QtQml/QQmlEngine>
4547
#include <QtGui/QStyleHints>
@@ -889,7 +891,16 @@ void Quick2ViewCmdExecutor::ExecuteScript(const std::string& script, const base:
889891
script.c_str(),
890892
args_as_json.c_str());
891893

892-
QQmlExpression expr(view->rootContext(), view, jscript.c_str());
894+
QQuickItem *rootItem = qobject_cast<QQuickItem*>(view->contentItem());
895+
QQmlContext *rootContext = view->rootContext();
896+
QVariant p = rootContext->contextProperty("ObjectNameUtils");
897+
898+
if (p.isNull()) {
899+
ObjectNameUtils* objn = new ObjectNameUtils(rootItem);
900+
rootContext->setContextProperty("ObjectNameUtils", objn);
901+
}
902+
903+
QQmlExpression expr(rootContext, view, jscript.c_str());
893904
QVariant result = expr.evaluate();
894905
if (expr.hasError()) {
895906
*error = new Error(kJavaScriptError, expr.error().toString().toStdString());

Diff for: wd_ext_qt.gyp

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
'src/webdriver/extension_qt/quick2_view_enumerator.cc',
111111
'src/webdriver/extension_qt/quick2_view_executor.cc',
112112
'src/webdriver/extension_qt/qml_view_util.cc',
113+
'src/webdriver/extension_qt/qml_objname_util.h',
114+
'src/webdriver/extension_qt/qml_objname_util.cc',
115+
'<(INTERMEDIATE_DIR)/moc_qml_objname_util.cc',
113116
],
114117
} , {
115118

0 commit comments

Comments
 (0)