Results 1 to 3 of 3

Thread: Possible to use a widget in another widget's paintEvent?

  1. #1
    Join Date
    Sep 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Possible to use a widget in another widget's paintEvent?

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Possible to use a widget in another widget's paintEvent?

    Quote Originally Posted by robot_love View Post
    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.

  3. #3
    Join Date
    Sep 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Possible to use a widget in another widget's paintEvent?

    I'll try that. Thanks, Jacek.

Similar Threads

  1. QDockWidget inside another widget in the center?
    By Antebios in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 07:06
  2. Replies: 6
    Last Post: 3rd September 2008, 14:27
  3. Overlapping Widgets and the Application
    By csvivek in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2008, 16:32
  4. painting a widget outside a paintEvent
    By jayw710 in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2007, 23:18
  5. When is the best time to delete a QCanvasItem
    By irudkin in forum Qt Programming
    Replies: 12
    Last Post: 8th March 2007, 21:28

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.