title: Dynamically adding widgets to gridLayout Qt
body: <p>I am having a bit of difficulty with some code. I am super rather new to Qt so it is entirely possible that I am simply ignorant to the problem I am having. </p>
  
  <p>Basically, I am blocking out a program so that I can add the specifics of it later. I want to be able to create a grid of buttons, and when one of those buttons is pressed, another shape to replace it. </p>
  
  <p>I am able to make my button grid, have it be scrollable, and have the button call it its position on the grid when pressed. However, when I try and use those coordinates to add another button to the grid, Qt crashes. </p>
  
  <p>Here's my code so far:</p>
  
  <p>mainwindow.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  #include &lt;cmath&gt;
  #include &lt;QLabel&gt;
  #include &lt;QMainWindow&gt;
  #include &lt;QVBoxLayout&gt;
  #include &lt;QGridLayout&gt;
  #include &lt;QApplication&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QScrollArea&gt;
  #include &lt;QDebug&gt;
  #include &lt;QString&gt;
  #include &lt;QSignalMapper&gt;
  #include &lt;QStringList&gt;
  #include &lt;QLayoutItem&gt;
  
  
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
   {
      ui-&gt;setupUi(this);
      populateViewGrid(); //I wanted to see if I could add in a scrollbar 
                          //from outside the main window. Could this be causing 
                          // the issue?
   }
  
  void MainWindow::populateViewGrid()
  {
  QScrollArea*scrollArea = new QScrollArea(this);
  QWidget*central = new QWidget(this);
  QGridLayout*gridLayout = new QGridLayout(central);
  QSignalMapper *signalMapper = new QSignalMapper(central);
  for (int i = 0; i &lt; 3; i++)
  {
      for (int j = 0; j &lt; 3; j++) 
      {
          QString position= QString("%1,%2").arg(i).arg(j);
          QPushButton* button = new QPushButton("addTrack",central);
          gridLayout-&gt;addWidget(button, i, j);
          connect(button, SIGNAL(clicked()),signalMapper, SLOT(map()));
          signalMapper-&gt;setMapping(button, position);
      }
  }
  
  connect(signalMapper, SIGNAL(mapped(QString)),this, SLOT(addTrack(QString )));
  
  central-&gt;setLayout(gridLayout);   
  
  scrollArea-&gt;setWidget(central);
  setCentralWidget(scrollArea);
  }
  
  
  
  void MainWindow::addTrack(QString position)
  {
  
  QStringList query = position.split(",");
  int x;
  x=query.at(0).toInt();
          int y;
          y=query.at(1).toInt()    ;
  
  QPushButton *Ifthisworks=new QPushButton(this);
  
  //This first line is where is crashes. I know this due to having the code
  //laced with qDebugs. From all of my google searches and such, it seems that 
  // something simple should be wrong and I can't find it. 
  
  QLayoutItem * existingitem = gridLayout-&gt;itemAtPosition(x, y);
  
  
  if(existingitem) {
      gridLayout-&gt;removeItem(existingitem);
      delete existingitem;
  }
  // before I included the above to remove the button from the grid point, the 
  //program would crash here. 
  
  gridLayout-&gt;addWidget(Ifthisworks, x, y);
  
  }
  
  
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "mainwindow.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>mainwindow.h</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  #include &lt;cmath&gt;
  #include &lt;QLabel&gt;
  #include &lt;QMainWindow&gt;
  #include &lt;QVBoxLayout&gt;
  #include &lt;QGridLayout&gt;
  #include &lt;QApplication&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QMainWindow&gt;
  #include &lt;QScrollArea&gt;
  #include &lt;QSignalMapper&gt;
  #include &lt;QHash&gt;
  //unrelated question, do I need the above in my code? I know not all of them 
  //used, but do I need includes in this file as well?
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
      void populateViewGrid();
  QGridLayout *gridLayout;
  
  public slots:
  
  void addTrack(QString);
  private:
      QScrollArea*scrollArea;
      QWidget * central;
      QPushButton *Ifthisworks;
      QSignalMapper *signalMapper;
      QPushButton *clockViews;
  
      Ui::MainWindow *ui;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>If you could help me understand how to not make Qt crash there and also add a button, that would be fantastic =)</p>
  
  <p>So some background real quick incase you are looking at my code and are scratching your head. I'm a mechanical engineer who should have probably been an electrical or computer engineer and knows enough about coding to get myself into these kinds of messes. For the most part, I searched for what I wanted Qt to do and hacked it all together to hopefully make it work. Any bit of deeper understanding you can share would be more than welcome.</p>
  
  <p>Thank you for your time. </p>