Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget and it's children
QtWS25 Last Chance

QWidget and it's children

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 7.7k Views
  • 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 Offline
    M Offline
    mans.zigher
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mans.zigher
      wrote on last edited by
      #2

      Maybe I should mention that I am running on Linux.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        Could you produce a testcase?

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

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          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
          0
          • T Offline
            T Offline
            tobias.hunger
            wrote on last edited by
            #5

            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
            0

            • Login

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