QWidget and it's children

  • Hi,

    I am trying to get children of a QWidget but without success. I have written a small program with a mainwindow and a button. I have also written a library defining a QApplication class with a exec function. When running my program I run it with the LD_PRELOAD=/path/to/library ./TestApp this will make TestApp to run my QApplication::exec() method instead of the original QApplication::exec(). In my exec() I then initiate some code and then call the original exec(). So far so good. The code that I have initiated in my exec function is a class with a slot that will be called by a QTimer the slot looks like below
    void Dumper::dump()
    qDebug() << "dump!!!";
    QList<QWidget*> list = qobject_cast<QApplication > (qApp)->allWidgets();
    if (!list.empty()) {
    qDebug() << "========START==========";
    > it(list);
    QWidget *widget;
    while (it.hasNext()) {
    widget = it.next();
    QObject *object = widget;
    qDebug() << "====================";
    qDebug() << "Window:" << widget->isWindow();
    qDebug() << "Widget:" << widget->isWidgetType();
    qDebug() << "Visible:" << widget->isVisible();
    qDebug() << "Width:" << widget->width();
    qDebug() << "Height:" << widget->height();
    qDebug() << "X:" << widget->geometry().x();
    qDebug() << "Y:" << widget->geometry().y();
    qDebug() << "Size:" << widget->geometry().size();
    qDebug() << "Title:" << widget->windowTitle();
    qDebug() << "Accessible Name:" << widget->accessibleName();
    qDebug() << "Object Name:" << widget->objectName();
    QPushButton *button = widget->findChild<QPushButton >("pushButton");
    if (button != NULL) {
    qDebug() << "found button";
    QObjectList objects = widget->children();
    if (objects.isEmpty()) {
    > oit(objects);
    qDebug() << objects.count();
    while (oit.hasNext()) {
    QObject *obj = oit.next();
    qDebug() << "Text:" << obj->property("text").toString();
    qDebug() << "Class Name:" << obj->metaObject()->className();
    } else {
    qDebug() << "empty list";
    qDebug() << "Text:" << object->property("text").toString();
    qDebug() << "Class Name:" << object->metaObject()->className();
    qDebug() << "========DONE==========";

    When calling findChild() on the widget class I get a QPushButton pointer returned so there is apparently a child of the widget named "pushButton". But when calling children() of the same widget the list is empty what can I do to get a list of all the childrens of a widget what is't that I am doing wrong? Any idea?

  • Maybe I should mention that I am running on Linux.

  • Could you produce a testcase?

  • Line 28 is wrong, it should read

    if (!objects.isEmpty()) {

    i.e. if objects is not empty - you miss the "!".

    Some more tips:

    As each QWidget is always a QObject, you can save yourself the cast and call all the QObject methods on the widget directly.

    If you do not have special need of the iterators, it might be shorter to write your loops this way:

    foreach(QWidget *widget, list) {

    // resp.
    foreach(QObject *obj, objects) {

  • Moderators

    If you want to examine the QObject tree(s) of an application you could just use this: https://gitorious.org/basyskom-inspector

    It does have some bugs with newer Qt versions and could use an update, but I still find the tool very helpful.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.