Results 1 to 10 of 10

Thread: How to change shape fast

  1. #1
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to change shape fast

    Hi,
    I am using GraphicsView to display 15,000 > rectangle I want to change the shapes dynamically to Circle, Square or Triangle etc. and vice versa
    Which is the fastest way to do this ???
    Last edited by nileshsince1980; 17th October 2007 at 13:13.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    The most efficient way is to have subclassed QGraphicsItem's.
    All items would have multiple QPainterPath members, one for each shape you want them to have.

    Then you could iterate through all items in the scene and call a method, let's say switchShape(CustomItem::Circle).
    This function would change the shape returned by CustomItem::shape() to the one that represents the circle.

    After you finished iterating through all the items you can issue an update on the scene, so the items get updated and the new shapes will be painted.

    You can update after you call switchShape for every item, but that introduces extra overhead.

  3. #3
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    Hi
    I have tried to override QGraphicsItem's class and changed shape in Paint function.
    But after settign the shape for each item I am calling scene's update..but update is taking time reflect the changes.
    How to make it fast ??

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    Call update only once, after you change the shapes for all items in the scene.

  5. #5
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    I have called only once update..after setting the shape of all items.
    but still it is slow
    sample code is like this
    for (QList<QGraphicsItem *>::iterator it = l.begin(); it != l.end(); ++it)
    {
    GraphicsItem *item = *it;
    item->setShape(ShapeType);
    }
    scene->update();

    //In GraphicsItems class
    GraphicsItems::Paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
    {
    QPainterPath shape;

    painter->setPen(penType);
    painter->setBrush(color);

    switch(ShapeType)
    {
    case CircleType:
    shape.addPath(circle);
    shape.closeSubpath();
    break;

    case SquareType:
    shape.addPath(square);
    shape.closeSubpath();
    break;

    case TriangleType:
    shape.addPath(triangle);
    shape.closeSubpath();
    break;

    default:
    shape.addPath(circlePath);
    shape.closeSubpath();
    }

    painter->drawPath(shape);
    }

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    What about setShape? Do you perform any updates there?

  7. #7
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    No In SetShape(..) i just initilaise the type for shape in class data member. and later in Paint(..) draws the shape accordingly.
    GraphicsItems::setShape(int type)
    {
    ShapeType = type;
    }

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    I think you should cache the shapes and just set them when you paint.
    Currently you are constructing the shapes for every paint, and that takes some time for 15k items.

  9. #9
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to change shape fast

    Changing shapes of many items at once incurs lots of changes in bsp indices.

    If you are changing shape of many items at one time, the first thing to do should be disble bsp indexing temporarily.

    Secondly set the view's update mode to FullScreenUpdateMode(again temporarily)

    Thirdly find out what all optimization flags you can set for the view.

    Then also if the performance is not ok, start caching the pixmaps.
    For eg if most of items look identicle, you can use one pixmap to draw those items that too rapidly.

    Also setting QGlWIdget as viewport might have some impact.
    To really identify bottlenecks, you should profile your app.
    Last edited by Gopala Krishna; 17th October 2007 at 14:45.
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  10. #10
    Join Date
    Sep 2007
    Location
    Pune, India
    Posts
    60
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change shape fast

    I have cached the shapes, All circle, square, triangle varialbe are of type PainterPath, I have created them in GraphicsItem's constructor. & depending upon the selected type of shape I have drawn in Paint function.

    What do you mean by caching shapes exactly ???
    Please can you more elaborate it ??

Similar Threads

  1. statusBar() message color change
    By mclark in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 23:20

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.