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

Adding a widget to its own layout results in endless loop

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P4: Low
    • None
    • 4.8.5, 5.3.1
    • Widgets: Layout
    • None
    • Gentoo Linux, mostly stable, with additional kde and qt repositories through layman
    • 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

        For Gerrit Dashboard: QTBUG-40609
        # Subject Branch Project Status CR V

        Activity

          People

            Unassigned Unassigned
            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