Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QWidget and it's children

    General and Desktop
    4
    5
    7274
    Loading More Posts
    • 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.
    • M
      mans.zigher last edited by

      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==========";
      QListIterator<QWidget
      > 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()) {
      QListIterator<QObject
      > 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?

      1 Reply Last reply Reply Quote 0
      • M
        mans.zigher last edited by

        Maybe I should mention that I am running on Linux.

        1 Reply Last reply Reply Quote 0
        • D
          dangelog last edited by

          Could you produce a testcase?

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply Reply Quote 0
          • G
            goetz last edited by

            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) {
            @

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply Reply Quote 0
            • T
              tobias.hunger last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • First post
                Last post