title: Editing a vector with generated spinboxes in QT
body: <p>I am attempting to modify a vector of doubles with qdoublespinboxes generated by a class. I've tried many different solutions but I cannot figure out how to get the variable &quot;i&quot; inside of the connect lambda function. This is my first time dynamically creating ui elements in QT, so my approach may be very off.</p>
  <p><strong>vector_editor.h</strong></p>
  <pre><code>#ifndef VECTOR_EDITOR_H
  #define VECTOR_EDITOR_H
  
  #include &lt;QGridLayout&gt;
  #include &lt;QWidget&gt;
  #include &lt;QDoubleSpinBox&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  class vector_editor
  {
  public:
      vector_editor(QWidget* widget, vector&lt;double&gt;&amp; v);
      QGridLayout* grid;
  };
  
  #endif // VECTOR_EDITOR_H
  </code></pre>
  <p><strong>vector_editor.cpp</strong></p>
  <pre><code>#include &quot;vector_editor.h&quot;
  
  vector_editor::vector_editor(QWidget* widget, vector&lt;double&gt;&amp; v)
  {
      grid = new QGridLayout(widget);
  
      for(uint i=0;i&lt;v.size();i++){
          QDoubleSpinBox* spinbox = new QDoubleSpinBox(widget);
          spinbox-&gt;setValue(v[i]);
          grid-&gt;addWidget(spinbox,i,0);
          QObject::connect(spinbox, QOverload&lt;double&gt;::of(&amp;QDoubleSpinBox::valueChanged), [&amp;](double d){
              v[i] = d;
          });
      }
  }
  </code></pre>
  <p><strong>mainwindow.cpp</strong></p>
  <pre><code>#include &quot;mainwindow.h&quot;
  #include &quot;ui_mainwindow.h&quot;
  
  #include &quot;vector_editor.h&quot;
  
  MainWindow::MainWindow(QWidget *parent)
      : QMainWindow(parent)
      , ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      vector&lt;double&gt; v = vector&lt;double&gt;{1,2,3};
      vector_editor(ui-&gt;widget,v);
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  </code></pre>
  <p>Whenever I attempt to change the value of one the spinboxes, the program crashes because the &quot;i&quot; variable is not yet initialized inside of the lambda function.</p>