title: How to fix QTableWidget setText/setCellWidget causing crash when loading QSettings
body: <p>Hello I'm using a <code>QTableWidget</code> and essentially have code such that the first column will allow the table to grow/shrink dynamically. The last populated row's first cell will have a Plus button to add a new row that can be edited and the Minus button will remove its corresponding button's row. For example, If I click the Plus button 5 times, it will be at the 6th row and the previous 5 rows will all have a Minus buttons to remove their row. If the cell's row has a Minus button on it, the row is implied editable, otherwise not. However, I allow the table to have a default amount of rows, such that it can never visually shrink less than 5, for example, even if it's technically "empty". To edit the row, you need to hit the Plus button which will check if we need to insert a new row, for example if we're at 5 rows and the base row size is 5, then we need a new row and to populate each column's index of the new row. </p>
  
  <p>The main reason for this table is to read and write the table's information to a <code>QSettings</code> and be able to import the past exported information to the same cells and essentially restore the table's state as it appeared last. However, when reading from the <code>QSettings</code>, the table will crash after all cells are populated correctly with the information and I can tell they are correctly populated because the debugger will freeze the GUI and I can visually see and <code>qDebug()</code> how far into the loop of the <code>QSettings</code> array I got to before seg-faulting.</p>
  
  <p>Now when I do everything manually, such as clicking the Plus button, everything is fine and the program performs as expected.</p>
  
  <pre><code>//newRowCount is assumed to be the amount of rows with Minus Buttons currently, only creates a new row if the table needs to expand
  void TableTest::enableNewRow(){
      newRowCount++;
      if(newRowCount &gt; table-&gt;rowCount()){
          table-&gt;insertRow(table-&gt;rowCount());
          for(int i = 0; i &lt; table-&gt;columnSize(); ++i){
              QTableWidgetItem* item = new QTableWidgetItem;
              item-&gt;setFlags(item-&gt;flags() ^ Qt::ItemisEditable);
              table-&gt;setItem(newRowCount, i, item);
              //Code to set the previous column's item (newRowCount--) to have an Qt::ItemisEditable flag set to true
              ...
          }
      }
          //Some button setup to make Plus button and create a new Minus button connect and do their jobs and move the Plus button down into the newly inserted row
          ...
  }
  void MainWindow::importFile(){
      int settingRows = settings-&gt;beginReadArray("Groups");
      for(int i = 0; i &lt; settingRows; ++i){
          settings-&gt;setArrayIndex(i);
          //Will only add a new row if needed, check inside above function and all row items will be allocated if made
          table-&gt;enableRow();
          for(int j = 1; j &lt; table-&gt;columnCount(); ++j){
              table-&gt;item(i, j)-&gt;setText("testing");
          }
      }
  }
  
  And the program crashes with the following stack trace:
  1  QWidget::show()
  2  QAbstractItemView::updateEditorGeometries()
  3  QAbstractItemView::updateGeometries()
  4  QTableView::updateGeometries()
  5  QTableView::timerEvent(QTimerEvent *)
  6  QObject::event(QEvent *)
  7  QWidget::event(QEvent *)
  8  QFrame::event(QEvent* )
  9  QAbstractScrollArea::event(QEvent* )
  10 QAbstractItemView::event(QEvent* )
  11-21 ... Not useful information about any of the code
  22 QCoreApplication::exec()
  23 main
  </code></pre>
  
  <p>However, it's all byte code and I can't actually inspect which line is crashing the code in any of the traces using the debugger. What I don't understand is that the same process for manually clicking the Plus button is being completed (I connect the click event of the Plus button to the <code>enableRow()</code> function) and when I programmatically call <code>enableRow()</code> from the <code>QSettings</code> loop in <code>importFile()</code> it will crash after it loops through all of the items. </p>
  
  <p>Things to note: setting the base size of the table to the size of the QSettings array I'm reading from (i.e. if I want 10 rows, just setting 10 rows to begin with before setting the item texts will work correctly), however, I want the base size of the table upon creation to be something like 5. It seems once the loop goes beyond the row count I specified from the constructor, then the program will crash, but not at say, index 6, but only after it has completely looped through the table. It does not crash on any line such as <code>QTableWidgetItem::setText</code> or <code>QTableWidget::setCellWidget()</code>. I'm kind of confused and wondering if the table is populating too quickly for the <code>QTableWidget::timerEvent</code> on stack trace line 5. Nothing I do in the loop is any different than what I do when manipulating the table normally.</p>