Results 1 to 7 of 7

Thread: Slow in drawing arraws

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Slow in drawing arraws

    Hi,

    I need to draw 12,000 arrows pointing to different directions depending on data. It take very long time (about 1.5 seconds) to finish.

    Following is the codes. If I comment out painter->drawPolyon(...), it only takes 0.037 seconds. Is there a way to speed it up? Thanks!

    Qt Code:
    1. QRectF Rect( QPointF( 0, 0 ), QSizeF( 3, 3 ) );
    2. Rect.moveCenter( QPointF( 0, 0 ) );
    3.  
    4. QPolygonF arrow( 3 );
    5. arrow[ 0 ] = Rect.topLeft();
    6. arrow[ 1 ] = QPointF( Rect.right(), 0 );
    7. arrow[ 2 ] = Rect.bottomLeft();
    8.  
    9. painter->setBrush( Qt::black ); // fill with black on the arrow head
    10.  
    11. for ( int idx = 0; idx < dataSize; idx++ ) { // dataSize = 12,000
    12.  
    13. painter->save();
    14.  
    15. painter->translate( ... ) // translate to arrow location
    16. painter->rotate( ... ) // rotate to angle degree depending on data
    17.  
    18. QLineF line( QPointF( 0, 0 ), QPointF( length, 0 ) ); // length is calculated from data
    19. painter->drawLine( line );
    20. painter->drawPolyon( arrow.translated( length, 0 ) );
    21.  
    22. painter->restore();
    23. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Slow in drawing arraws

    You can obviously replace the polygon with three line segments, which should, based on your timings, be much faster. But you will get no fill in the arrow.

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slow in drawing arraws

    Quote Originally Posted by mvuori View Post
    You can obviously replace the polygon with three line segments, which should, based on your timings, be much faster. But you will get no fill in the arrow.
    If I remove painter->setBrush( Qt::black ) and keep other the same, the drawing is also taking no time. So the problem is the fill, but I need to fill the arrow...

  4. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Slow in drawing arraws

    1. painter->save(); painter->restore(); - this is costly avoid this when it is possible.
    2. Use QPixmap to draw arrow fast

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Slow in drawing arraws

    As MArekR22 suggested:
    If the head of the arrows is always the same, and you only need different lengths for the "root" of the arrow, than you can draw the head once on a QPixmap, and just translate the pixmap, and draw it.
    This should get rid of the filling, which is costly.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Slow in drawing arraws

    I need to draw 12,000 arrows pointing to different directions
    Sorry for offtopic, but I think it's more than too much for any human being
    To add to what's already been said, you can cache the root lengths (in fact cache the calculated data) and draw complete arrow pixmaps.

  7. #7
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Slow in drawing arraws

    Quote Originally Posted by high_flyer View Post
    As MArekR22 suggested:
    If the head of the arrows is always the same, and you only need different lengths for the "root" of the arrow, than you can draw the head once on a QPixmap, and just translate the pixmap, and draw it.
    This should get rid of the filling, which is costly.
    I have tried using both pixmap and image, and it takes more time!

    Now, I am using another way, which take 0.04 seconds! Obviously, drawing points with large pen and clip it using the arrow is fast!

    Qt Code:
    1. painter->save();
    2. painter->translate( pos );
    3.  
    4. painter->rotate( degreeAngle - 90 );
    5.  
    6. QLineF line( QPointF( 0, 0 ), QPointF( length, 0 ) );
    7. painter->drawLine( line );
    8.  
    9. QPen pen = painter->pen();
    10. pen.setWidth( d->penSize ); // here d->penSize = 100;
    11. painter->setPen( pen );
    12.  
    13. path.addPolygon( d->arrow.translated( length, 0 ) );
    14. painter->setClipPath( path );
    15. painter->drawPoint( QPointF( length, 0 ) );
    16.  
    17. painter->restore();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 8th April 2010, 16:06
  2. Slow interface
    By Guilo in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2010, 17:32
  3. my setupUi is very slow !
    By simnav in forum Qt Programming
    Replies: 1
    Last Post: 5th February 2009, 04:17
  4. Qt4 designer SLOW - how to fix?
    By hvengel in forum Qt Tools
    Replies: 3
    Last Post: 10th January 2008, 12:07
  5. Drawing antialiased text on Windows very slow
    By Rawk in forum Qt Programming
    Replies: 13
    Last Post: 14th May 2007, 14:13

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
  •  
Qt is a trademark of The Qt Company.