Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Element and Event inspection in QML (solved)
Forum Updated to NodeBB v4.3 + New Features

Element and Event inspection in QML (solved)

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 4 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pedromateo
    wrote on last edited by
    #1

    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!!!

    Pedro Mateo
    pedromateo@um.es
    http://www.pedromateo.es

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hhartz
      wrote on last edited by
      #2

      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 :)

      --

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aalpert
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pedromateo
          wrote on last edited by
          #4

          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!!!

          Pedro Mateo
          pedromateo@um.es
          http://www.pedromateo.es

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mbrasser
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pedromateo
              wrote on last edited by
              #6

              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!!

              Pedro Mateo
              pedromateo@um.es
              http://www.pedromateo.es

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved