I have a program that will allow people to track certain tasks that they are working on. I have stored the tasks in a list, and have created a widget (called "taskWidget") which I would like to use to represent each task on the list. I would like a widget instead of a regular row to make editing/comprehension easier.

I have created a scrollArea as my central widget and created a custom widget as my TaskView.

From what I understand, in TaskView I need to re-implement paintEvent(). My question is: how do I create and paint my taskWidget from inside paintEvent()?

I have used Mark Summerfield's excellent Rapid GUI Programming, and am basing my paintEvent off of his WaterModelView in chapter 16. I have spent 4 days scouring this forum, the internet, and PyQt's mailing lists, but cannot get anything to paint. I feel like I'm missing something fundamental.

I started with a QItemDelegate, and got my widget to appear as an editor, but not as the underlying task. I then moved away from QItemDelegate to subclassing QWidget and adding it to the scrollArea, so I know it needs to handle all painting inside that scrollArea.

Any help that can be provided would be greatly appreciated. It's in PyQt, but I understand enough C++ to get the gist of things if that helps anyone to respond.

Qt Code:
  1. def paintEvent(self, event):
  2. if self.model is None:
  3. return
  4. task = TaskWidget("999-999",
  5. "THIS IS A RATHER LONG TASK NAME",
  6. 99999,
  7. False)
  8. size = task.height()
  9. minY = event.rect().y()
  10. maxY = minY + event.rect().height() + size
  11. minY -= size
  12. painter = QPainter(self)
  13. painter.setRenderHint(QPainter.Antialiasing)
  14. painter.setRenderHint(QPainter.TextAntialiasing)
  15.  
  16. y = 0
  17. for row in range(self.model.rowCount()):
  18. if minY <= y <= maxY:
  19. painter.save()
  20. t = TaskWidget(self.model.tasks[row].studyNumber,
  21. self.model.tasks[row].taskName,
  22. self.model.tasks[row].timeInMinutes)
  23. painter.initFrom(t)
  24. painter.restore()
  25. y += size
  26. if y > maxY:
  27. break
To copy to clipboard, switch view to plain text mode