You need to disable sorting while you are setting / inserting the items.
Qt Code:
  1. ...
  2. this->setSortingEnabled(false);
  3. this->setItem(...);
  4. this->setSortingEnabled(true);
  5. ...
To copy to clipboard, switch view to plain text mode 
This is because as soon as you set/insert an item, table will automatically sort again and the just inserted item will be moved to some new row, but you will still try use the old row number, and things get messed up and your program may even crash.