title: Cannot add items to a QTableWidget
body: <p>I have a program that queries a SQLite database and displays some of the information from this database in a table.  I've done this a few times in the program, and sorted out a more ambiguous way to do it, so I wrote up a new functions, but it always throws an assertion:</p>
  
  <pre><code>ASSERT failure in QList&lt;T&gt;::operator[]: "index out of range", file /usr/include/qt/QtCore/qlist.h, line 462
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>void Sync::on_btnLoadTable_clicked()
  {
  if ( !m_district ) return;
  
  ui-&gt;tblTableView-&gt;clear();
  ui-&gt;tblTableView-&gt;setRowCount( 0 );
  ui-&gt;tblTableView-&gt;setColumnCount( 0 );
  
  ui-&gt;tblTableView-&gt;setSortingEnabled( false );
  
  QStringList fields;
  QList&lt; QStringList &gt; rows = m_district-&gt;getAllOf( ui-&gt;cboTables-&gt;currentText(), &amp;fields );
  
  int rowCount = rows.size();
  if ( rowCount &gt; 0 )
  {
      int columns = fields.size();
      ui-&gt;tblTableView-&gt;setColumnCount( columns );
      for(int c=0; c &lt; columns; c++)
          ui-&gt;tblTableView-&gt;setHorizontalHeaderItem( c, new QTableWidgetItem(fields[c]) );
  
      ui-&gt;tblTableView-&gt;setRowCount( rowCount );
      for(int r=0; r &lt; rowCount; r++)
      {
          int cols = rows[r].size();
          qDebug() &lt;&lt; "Fields=" &lt;&lt; columns &lt;&lt; ", row columns=" &lt;&lt; cols;
          qDebug() &lt;&lt; "Table is " &lt;&lt; ui-&gt;tblTableView-&gt;columnCount() &lt;&lt; "x" &lt;&lt; ui-&gt;tblTableView-&gt;rowCount();
          for(int c=0; c &lt; columns; c++)
          {
              qDebug() &lt;&lt; "row column count=" &lt;&lt; rows[r].size();
              qDebug() &lt;&lt; "row=" &lt;&lt; r &lt;&lt; ",col=" &lt;&lt; c;
              QString data = rows[r][c];
              qDebug() &lt;&lt; "text=" &lt;&lt; data;
              QTableWidgetItem *item = new QTableWidgetItem( data );
              ui-&gt;tblTableView-&gt;setItem( r, c, item );
              qDebug() &lt;&lt; "inserted [" &lt;&lt; r &lt;&lt; "," &lt;&lt; c &lt;&lt; "]";
          }
      }
      qDebug() &lt;&lt; "Finished";
  }
  ui-&gt;tblTableView-&gt;setSortingEnabled( true );
  
  }
  </code></pre>
  
  <p>And it outputs the following in qDebug:</p>
  
  <pre><code>Fields= 10 , row columns= 10 
  Table is  10 x 6381
  row column count= 10 
  row= 0 ,col= 0 
  text= "137288735238a076b1ed644781a8cdf3f598067b41" 
  ASSERT failure in QList&lt;T&gt;::operator[]: "index out of range", file /usr/include/qt/QtCore/qlist.h, line 462
  </code></pre>
  
  <p>I put up a series of breakpoints, and it appears that it breaks on the <code>ui-&gt;tblTableView-&gt;setItem( r, c, item );</code> line, but the column and row count seem to imply that row <code>r</code> and column <code>c</code> are both valid places to put an item.</p>
  
  <p>Any ideas why it would be giving me this assertion?</p>