Qt with PyQt. Is it possible to view the object hierarchy of a running window?
<p>I am currently working on a project using PyQt5, and am using several windows that will dynamically change their widgets, depending on the data I supply.</p>

<p>For the purposes of debugging, would it be possible to view the object structure of a window as it is running? I am using Qt Designer to design many of my windows, but as the contents change while the program is running, Qt Creator cannot show me the structure of my windows at all times in my program.</p>

<p>Is it possible to view the object hierarchy, such as in the Object Inspector in Qt Creator? Or perhaps is it possible to make the invisible widgets, like layouts and spacers, visible while running?</p>

<p>I don't know if it's possible to do this in Qt Creator while the application is running, but you can definitely insert statements into the source code which will print out the entire object hierarchy. Just pick your base object and recursively print the output of <code>QWidget.children()</code>.</p>

<p>One problem is when to print this so that you neither miss newly-added widgets nor completely overwhelm the terminal (so a timer is not the best idea). If your application responds to that data that you supply by calling a specific slot or function, which seems like the points at which the tree changes, that would be a perfect time to do this. You can start from the <code>QApplication</code> object, and walk the tree down from there.</p>

<p>If you don't respond to data that way, there is a more involved but OK solution. You can instead intercept all <code>QChildEvent</code>s and print the new object hierarchy at that point. This can be done by installing a filter object on the root object you're interested in as soon as it's created, which would then install itself on any new children. This could then print the full object tree, starting from the root. Check out <a href="https://stackoverflow.com/questions/39155748/how-to-receive-event-whenever-a-qwidget-is-added-to-qapplications-widget-tree/39156966#39156966">this answer</a> for more details on how to create this kind of filter object. it's in C++, but the code should be straightforward to translate to Python.</p>

python qt pyqt