PDA

View Full Version : Possible to use a widget in another widget's paintEvent?



robot_love
8th September 2008, 19:28
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.


def paintEvent(self, event):
if self.model is None:
return
task = TaskWidget("999-999",
"THIS IS A RATHER LONG TASK NAME",
99999,
False)
size = task.height()
minY = event.rect().y()
maxY = minY + event.rect().height() + size
minY -= size
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setRenderHint(QPainter.TextAntialiasing)

y = 0
for row in range(self.model.rowCount()):
if minY <= y <= maxY:
painter.save()
t = TaskWidget(self.model.tasks[row].studyNumber,
self.model.tasks[row].taskName,
self.model.tasks[row].timeInMinutes)
painter.initFrom(t)
painter.restore()
y += size
if y > maxY:
break

jacek
9th September 2008, 00:02
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.
If you add widgets to scroll area, they should draw themselves. Just don't forget to show() them.

robot_love
9th September 2008, 05:18
I'll try that. Thanks, Jacek.