title: Qt Segmentation Fault QTableWidget->SetItem(int,int,QTableWidgetItem)
body: <p><strong>FIXED</strong></p>
  
  <p>I'm writing a program that loads a file with 400 characters, and depending on the character changes the backgroundcolor of a tablecell on load.</p>
  
  <p>This is how I create the QTableWidget:</p>
  
  <pre><code>QTableWidget* mapTableWidget = new QTableWidget(this);
  mapTableWidget-&gt;setRowCount(20);
  mapTableWidget-&gt;setColumnCount(20);
  for (int i = 0; i&lt;20; i++)
  {
      mapTableWidget-&gt;setRowHeight(i,24);
      mapTableWidget-&gt;setColumnWidth(i,24);
  }
  mapTableWidget-&gt;setShowGrid(false);
  mapTableWidget-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);
  mapTableWidget-&gt;setSortingEnabled(false);
  </code></pre>
  
  <p>I add the widget to the current layout with a simple: </p>
  
  <pre><code>QVBoxLayout* layout = new QVBoxLayout;
  layout-&gt;setMargin(5);
  layout-&gt;addWidget(mapTableWidget);
  </code></pre>
  
  <p>And this is where it goes wrong: </p>
  
  <pre><code>for (int k=0; k&lt;20; k++)
  {
      for(int l=0; l&lt;20; l++)
      {
          QTableWidgetItem* tempitem = new QTableWidgetItem();
          tempitem-&gt;setBackgroundColor(mb-&gt;colorAt(l,k));
          mapTableWidget-&gt;setItem(k,l,tempitem);
      }
  }
  </code></pre>
  
  <p>The problem I have is that I keep running into a segmentation fault at mapTableWidget->setItem(k,l,tempitem) when debugging, and the application crashes at that point too.</p>
  
  <p>Does any one have any pointers (:]) to helping me out here?</p>
  
  <p>PS:</p>
  
  <p>This also happens when not using the color part, so commenting the setBackgroundColor line out (I know it's deprecated, but it worked on a different project) doesn't change anything.  </p>
  
  <p><strong>FIX</strong></p>
  
  <p>The QTableWidget was allocated locally and was outside the scope where the additem had access to. Defining the table higher was the fix.</p>