Inspecting Qt’s object hierarchy

Coming from web development, one of the most frustrating things about writing QSS (Qt’s version of CSS) is the lack of an inspector. In a browser, it’s trivial to design selectors by simply pawing through the DOM. Qt arranges its QObjects in a similar hierarchy, but the only way I’ve found to display it (in its entirety) is to load everything into QtCreator. This is slow and sometimes painful, when all I want is a simple text dump. So I built a small tool specifically for recursing a given QObject, and dumping it’s hierarchy to the terminal. Here is an example of using it to print the structure of an entire Qt application:

#include "mainwindow.h"
#include <QApplication>
#include "inspect.h" //you'll need this

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    MainWindow win;
    inspect(&win); //prints hierarchy to stderr
    win.show();

    return app.exec();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s