title: Fix tab order after inserting widgets in a layout in QT
body: <p>I have a custom list implementation (a subclass of <code>QWidget</code>) in QT 5.5. The elements of the list are organized using a <code>QVBoxLayout</code>. At runtime, elements (which are also <code>QWidget</code>s) can be dynamically added to and removed from the list at any position in the layout. This is working fine, except for one detail: the tab order of inserted focusable elements is wrong. The last element inserted will always be the last in the tab order, even if inserted in between two other elements.</p>
  
  <p>How can I fix the tab order to represent the layout order? I already tried iterating over the list elements and using <code>setTabOrder()</code> on each adjacent pair, without success.</p>
  
  <p>A few more details on the implementation:</p>
  
  <ul>
  <li>Widgets are not added directly to the list. Each time a widget should be added, a proxy widget is created and added instead, the 'real' widget will be reparented to the proxy (The proxy is doing some graphic stuff).</li>
  <li><code>QVBoxLayout::insertWidget()</code> is used to insert proxy widgets, followed by a call to <code>QWidget::show()</code></li>
  <li>when removing elements, the element will be hidden, removed from the proxy, the proxy is removed from the list layout and deallocated</li>
  <li>focusable widgets can be anywhere in the object tree of elements which are added to the list, they are not necessarily the elements themselves</li>
  </ul>
  
  <p><strong>Update: Added a MCVE!</strong></p>
  
  <p>The following minified example demonstrates the problem. For completeness, I also included the headers, main function, and .pro file. <strong>You can safely skip those files if you don't want to reproduce the issue, TabOrderTestWindow.cpp is the important one.</strong></p>
  
  <p><em>TabOrderTestWindow.cpp:</em></p>
  
  <pre><code>#include "TabOrderTestWindow.h"
  
  #include &lt;QVBoxLayout&gt;
  #include &lt;QPushButton&gt;
  
  // create a button inside a proxy widget
  QWidget* createButtonProxy(const QString&amp; caption, QWidget* parent) {
      QWidget* proxy = new QWidget(parent);
      QPushButton* button = new QPushButton(caption, proxy);
      proxy-&gt;setFocusProxy(button);
      return proxy;
  }
  
  TabOrderTestWindow::TabOrderTestWindow()
      : QWidget()
  {
      setMinimumHeight(200);
      setMinimumWidth(350);
  
      QVBoxLayout* layout = new QVBoxLayout(this);
  
      // create and add 3 buttons in order
      QWidget* button1 = createButtonProxy("button 1", this);
      QWidget* button2 = createButtonProxy("button 2", this);
      QWidget* button3 = createButtonProxy("button 3", this);
      layout-&gt;addWidget(button1);
      layout-&gt;addWidget(button2);
      layout-&gt;addWidget(button3);
  
      // now insert a fourth button in between the others - incorrect tab order!
      QWidget* buttonInbetween = createButtonProxy("button in between", this);
      layout-&gt;insertWidget(1, buttonInbetween);
  
      // attempt to correct tab order - not working, even with focus proxy set...
      setTabOrder(button1, buttonInbetween);
      setTabOrder(buttonInbetween, button2);
  }
  </code></pre>
  
  <p><em>TabOrderTestWindow.h:</em></p>
  
  <pre><code>#ifndef TABORDERTESTWINDOW_H
  #define TABORDERTESTWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  class TabOrderTestWindow : public QWidget
  {
      Q_OBJECT
  
  public:
      TabOrderTestWindow();
  };
  
  #endif // TABORDERTESTWINDOW_H
  </code></pre>
  
  <p><em>main.cpp:</em></p>
  
  <pre><code>#include "TabOrderTestWindow.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      TabOrderTestWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p><em>TabOrderTest.pro:</em></p>
  
  <pre><code>QT       += core gui
  
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  
  TARGET = TabOrderTest
  TEMPLATE = app
  
  
  SOURCES += main.cpp\
          TabOrderTestWindow.cpp
  
  HEADERS  += TabOrderTestWindow.h
  </code></pre>