Element and Event inspection in QML (solved)
-
Hi everyone,
I am doing some tests in Qt Quick, and i would like to inspect the elements composing the declarative interface, that is, to expose the QML items to the C++ code.
For example, if i have a declarative GUI composed of a rectangle and two images, i would like to have a list with three (or more) elements with these widgets (or items in Qt Declarative).
In Qt4 it is as easy as call the QApplication::allWidgets() method, bout i don't know how to do it in QML.
The same happens with event filters, so i can not get the events sent to these items.
Cheers!!!
-
Hi Pedro,
You can do this by using the QML inspector in Bauhaus. This might be changed later, our tools are evolving. From the C++ side you can use "QDeclarativeContext::contextProperty":http://doc.qt.nokia.com/4.7-snapshot/qdeclarativecontext.html#contextProperty with the id of an element - and query for it's children property, rinse and repeat :)
-
You can also traverse the QObject tree from the rootObject() returned by QDeclarative view. From C++, this might be easier than using the QDeclarativeContext to fetch the children property. However you will need to be cognizant of the distinctions between the QObject tree and the QGraphicsItem tree, and traverse the correct one.
"QObject::dumpObjectTree":http://doc.qt.nokia.com/4.7-snapshot/qobject.html#dumpObjectTree provides a similar result if you just need console output to see the elements and their hierarchy.
-
Hi,
first, thanks for your quick answer!!
Some background: We focus our research on not-invasive testing tools for GUIs. Until now, we have been working with classical user interfaces, analyzing the GUI elements and the user interaction using object introspection and GUI events analysis, respectively (see "http://catedrasaes.inf.um.es/trac/wiki/ProjectsOht":http://catedrasaes.inf.um.es/trac/wiki/ProjectsOht and "http://catedrasaes.inf.um.es/trac/wiki/ProjectsOhtPlus":http://catedrasaes.inf.um.es/trac/wiki/ProjectsOhtPlus). Despite our tools are always based on an open and general implementation, Qt has been chosen as the default windowing system.
Now, we are focusing our effort on declarative GUIs, and we want to implement those instrospection and analysis processes into declarative GUIs.
So, the alternative proposed by Henrik is not viable for us, because we do not know, a priori, the id of any element.
On the other hand, using the root object provided by QDeclarativeView is a good idea. But now the problem is that the objects in that tree has no objectName.
We need a way to identify the elements, and using their name is the best one. We have tried this code:@...
obj = (QObject*)view.rootObject();
printNode(obj,0);...
void printNode(QObject* node, int indent)
{
if (node)
{
std::string line = "";//indent for (int i = 0; i < indent; i++) { line += " "; } line += "- "; //node name //line += node->objectName().toStdString(); line += "class = "; line += node->metaObject()->className(); int id_index = node->metaObject()->indexOfProperty("id"); line += " :: index = " + boost::lexical_cast<std::string>(id_index); QMetaProperty id_prop = node->metaObject()->property(id_index); if (id_prop.isReadable()) { QVariant id_value = id_prop.read(node); line += " :: id = " + id_value.toString().toStdString(); } else { line += " :: id not readable."; } //print myself log(line); //print children foreach(QObject* obj,node->children()) { printNode(obj,indent + 1); } }
}
...@
But i can not access to the "id" property (the returned index is -1).
Do you know how to get this property?Thanks in advance for your help,
cheers!!! -
Hi Pedro,
"id" isn't an actual QMetaProperty property -- its something provided by the QML engine, for use within QML. For what you are doing, you may want to look at the (private) QDeclarativeDebug* classes (in src/declarative/debugging). You might also find the visual test tool (tests/auto/declarative/qmlvisual) and related infrastructure interesting.
-
Hi again,
we have already implemented item and event introspection in a QML declarative interface.
You can see the solution in the following link:
"http://catedrasaes.inf.um.es/trac/blog/pedromateo-13/07/2010-qml_event_item_introspection":http://catedrasaes.inf.um.es/trac/blog/pedromateo-13/07/2010-qml_event_item_introspection
Thank you very much for your help,
cheers!!