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

replaceWidget( widget, widget ) removes widget from layout instead of leaving it effectively untouched

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • P3: Somewhat important
    • 5.14.0 Alpha
    • 5.9.5
    • Widgets: Layout
    • None
    • Linux/X11
    • 2b7edd053404f4819abf0203bb9c762799849638 (qt/qtbase/dev)

    Description

      I wanted to have a widget inside a layout replaced based on user input. The logic for that looked something like this:

       

      qDebug() << layout.count(); // 1 (widget out of choosableWidget in layout)
      foreach ( widget, choosableWidgets ) {
          if ( layout.replaceWidget( widget, chosenWidget ) != 0 )
              break;
      }
      

      However, if chosenWidget is equal to the widget already in the layout, then replaceWidget effectively removes the widget instead of leaving it untouched. I find this behavior to be undocumented and unintuitive.

      Here is a minimal bug-reproducing example which made me realize, that the behavior depends on whether the layout is being used by a widget or not, which makes this even weirder:

      #include <QApplication>
      #include <QWidget>
      #include <QVBoxLayout>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QApplication app( argc, argv );
      
          auto const layout = new QVBoxLayout();
          auto const widget = new QWidget();
          layout->addWidget( widget );
      
      #define BUG
      #ifdef BUG
          auto const treeBox = new QWidget();
          treeBox->setLayout( layout );
      #endif
      
          qDebug() << layout->count(); // output: 1
          layout->replaceWidget( widget, widget, Qt::FindDirectChildrenOnly );
          qDebug() << layout->count(); // output: 0 if BUG defined else 1 !
      }

       

      Attachments

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

        Activity

          People

            smd Jan Arve
            uiohbgq uiohbgq
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes