In your FieldChanged() method, it is not necessary to create new QPen and QBrush instances using "new", since you are passing them as const references into the scene->addEllipse() method. As it stands now, you have a memory leak every time the FieldChanged method is executed because you new up the pen and brush and never delete them..

Simply declare QPen and QBrush instances on the stack and pass them in as is. Rewrite the code as follows:

Qt Code:
  1. void MainWindow::FieldChanged(int Column, int Row, int NewValue)
  2. {
  3. QColor penColor;
  4. QColor brushColor;
  5. switch(NewValue)
  6. {
  7. case 0:
  8. {
  9. penColor = QColor(127, 127, 127);
  10. brushColor = QColor(127, 127, 127);
  11. break;
  12. }
  13. case 1:
  14. {
  15. penColor = QColor(0, 0, 0);
  16. brushColor = QColor(0, 0, 0);
  17. break;
  18. }
  19. case 2:
  20. {
  21. penColor = QColor(255, 255, 255);
  22. brushColor = QColor(255, 255, 255);
  23. break;
  24. }
  25. default:
  26. {
  27. penColor = QColor(255, 0, 0);
  28. brushColor = QColor(255, 0, 0);
  29. break;
  30. }
  31. }
  32. scene->addEllipse(-325 + BorderWidth + GridWidth * Column, -325 + BorderWidth + GridWidth * Row, 50, 50, QPen( penColor ), QBrush( brushColor ) );
  33. }
To copy to clipboard, switch view to plain text mode 

And regarding QWidget (QObject) ownership: As a general rule, any time you new a QObject, then give it to another QObject that will be its parent, the parent takes control of the lifetime of the object and you do not have to call delete on it. Likewise, calling setupUI on an embedded UI pointer passes ownership of that to the widget for which the ui is being created, and the QObject ownership mechanism will take care of deleting the pointer when the widget goes out of scope. Double deletes will cause the crashes you experienced, and it's a clear sign that you either are trying to use a pointer that hasn't been initialized or you're trying to delete one that's already been deleted.

Any other type of pointer (like your own C++ objects) follow the normal C++ rules: if you new it, you must delete it.