Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-40609

Adding a widget to its own layout results in endless loop

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P4: Low
    • Resolution: Done
    • Affects Version/s: 4.8.5, 5.3.1
    • Fix Version/s: None
    • Component/s: Widgets: Layout
    • Labels:
      None
    • Environment:
      Gentoo Linux, mostly stable, with additional kde and qt repositories through layman
    • Commits:
      983dde1f2f3db76ab26e949d8c2f4f8b968b36be

      Description

      Consider the following example (valid both for Qt4 and Qt5):

      MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)

      { QWidget *centralWidget = new QWidget(this); QBoxLayout *boxLayout = new QVBoxLayout(centralWidget); boxLayout->addWidget(centralWidget, 1); setCentralWidget(centralWidget); }

      Widget "centralWidget" is added to layout "boxLayout" which is layouting just this widget. Obviously, this "addWidget" call does not make sense, but it may happen due to copy&paste errors or auto-complete madness in editors/IDEs.

      When compiling and running such a program as shown above, both with Qt 4.8.5 and 5.3.1, the program hogs the CPU and freezes from the user's perspective.

      Qt should not accept adding a widget to its own layout. Such a statement should be ignored and/or a warning should be printed to stderr, but silently accepting it and then just freezing is not an option.

      A brief look into Qt's source code lets me suggest to enhance static bool checkWidget(..) in qboxlayout.cpp for a "self-insertion" test.

      Maybe a more global solution is necessary, as QFormLayout is affected by a similar issue. A modified example using the following code snipplet shows the same behaviour:

      QFormLayout *formLayout = new QFormLayout(centralWidget);
      formLayout->addRow(centralWidget);

      In case you are going to fix this issue, don't forget to add a test to tst_qboxlayout.cpp and friends

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            tfischer Thomas Fischer
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes