Results 1 to 3 of 3

Thread: Best way to draw the same thing many times in a QGraphicsScene

  1. #1
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Best way to draw the same thing many times in a QGraphicsScene

    My basic question is this: If you have many graphic objects (e.g., red circles) to draw that are the same in appearance but different in location, is it better to have one composite QGraphicsItem that draws the shape in many locations, or to have one QGraphicsItem for every instance?

    Here's the context: I'm trying to draw grid lines for a musical sequencer (e.g., the vertical lines in this pic). These are just thin vertical lines occurring in a regular pattern.

    I've thought about making one composite QGraphicsItem whose paint() function draws all of the lines in a loop. The boundingRect() function would just return the scene rect, since the lines should appear to extend infinitely high and low, and should be repeated throughout the entire width of the scene rect. This is very simple to implement, but I wonder if it might slow things down because the painter will draw all of the lines in the entire scene whenever a repaint is performed, even if only a few of the lines are visible.

    I've also thought about having one QGraphicsItem for every line. I can use the flyweight pattern to share as much data as possible (e.g., pen, boundingRect) between the lines, but still, QGraphicsItem does require enough memory to cause a slow-down whenever the width of the scene is increased and more lines must be dynamically allocated.

    Can someone help clear up if one of these approaches is better than the other? Or maybe I should be doing something completely different? Thanks!

  2. #2
    Join Date
    Jan 2011
    Posts
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Best way to draw the same thing many times in a QGraphicsScene

    Hi,

    if it is only grid lines, you could also consider overwriting QGraphicsScene::drawForeground() (or, in your case,
    QGraphicsScene::drawBackground() since the gray bars should obviously be behind the scene items).

    - Andreas

  3. The following user says thank you to littletux for this useful post:

    wayfaerer (9th May 2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    66
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Platforms
    Windows

    Default Re: Best way to draw the same thing many times in a QGraphicsScene

    That's a great idea. I don't know why I didn't think of it.

Similar Threads

  1. How to Draw spline on QGraphicsScene
    By seguprasad in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 05:28
  2. To Draw Text at given position on QGraphicsScene
    By volcano in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2010, 08:16
  3. want to draw points in QGraphicsScene
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 18:14
  4. Replies: 5
    Last Post: 11th March 2008, 13:38
  5. Replies: 4
    Last Post: 25th April 2007, 16:54

Tags for this Thread

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.