Page 1 of 2 12 LastLast
Results 1 to 20 of 80

Thread: GraphicsView performance problems

Hybrid View

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

    Default Re: GraphicsView performance problems

    I tested with QLine but that didn't make a difference. Also i am not sure what the int cast does - i feel it just takes int part without rounding. But do you think this will make a difference since I am following one convention in all places in drawBackground() ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 28 Times in 27 Posts

    Default Re: GraphicsView performance problems

    Quote Originally Posted by Gopala Krishna View Post
    I tested with QLine but that didn't make a difference. Also i am not sure what the int cast does - i feel it just takes int part without rounding. But do you think this will make a difference since I am following one convention in all places in drawBackground() ?
    It does make a difference:
    Qt Code:
    1. void drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. const int gridSize = 25;
    4.  
    5. const int realLeft = static_cast<int>(std::floor(rect.left()));
    6. const int realRight = static_cast<int>(std::ceil(rect.right()));
    7. const int realTop = static_cast<int>(std::floor(rect.top()));
    8. const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
    9.  
    10.  
    11. const int firstLeftGridLine = realLeft - (realLeft % gridSize);
    12. const int firstTopGridLine = realTop - (realTop % gridSize);
    13.  
    14. QVarLengthArray<QLine, 100> lines;
    15.  
    16. for (int x = firstLeftGridLine; x <= realRight; x += gridSize)
    17. lines.append(QLine(x, realTop, x, realBottom));
    18. for (int y = firstTopGridLine; y <= realBottom; y += gridSize)
    19. lines.append(QLine(realLeft, y, realRight, y));
    20.  
    21.  
    22. painter->setPen(QPen(Qt::darkGreen,0));
    23. painter->drawLines(lines.data(), lines.size());
    24. }
    To copy to clipboard, switch view to plain text mode 

    As you see I make three changes
    a) integer instead of float
    b) using the containing integer rect as basis (i.e. the smallest integer rect that contains the float rect)
    c) I changed the "<" in your for loops to a "<=", i thing part of your corruption was that it did not always repaint the grid when the line was at the border of the exposed rect.


    The result: no more corruption of the grid...only weird color changes that I am trying to track down ;-)


    EDIT: Of course you now need:
    Qt Code:
    1. #include <cmath>
    To copy to clipboard, switch view to plain text mode 
    for the floor, and ceil to work ;-)
    Last edited by camel; 17th February 2007 at 12:34.

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

    Gopala Krishna (17th February 2007)

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

    Default Re: GraphicsView performance problems

    Quote Originally Posted by camel View Post
    ...
    The result: no more corruption of the grid...only weird color changes that I am trying to track down ;-)
    for the floor, and ceil to work ;-)
    As you said no more corruption but yeah even I am cracking my head a lot on how to remove those weird color changes.
    But I tell you I found one magic while experimenting. Try , its fun.

    change the bounding rect of Resistor to
    Qt Code:
    1. QRectF Resistor::boundingRect() const
    2. {
    3. qreal pw = 0.5;
    4. return QRectF(-47,-9,47*2,18).adjusted(-pw,-pw,pw,pw);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Check for performance now!!
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: GraphicsView performance problems

    I really suggest to recompile Qt with profiling information. You're shooting blind now. Profiling will give you the exact bottleneck point. Furthermore make sure your app is compiled correctly, I think you should be able to have a decomposition of the drawBackground() method in the profiling information as well. It should then tell you which part of the method takes most time and it should be possible to obtain without recompiling Qt.

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

    Default Re: GraphicsView performance problems

    Quote Originally Posted by wysota View Post
    I really suggest to recompile Qt with profiling information. You're shooting blind now. Profiling will give you the exact bottleneck point. Furthermore make sure your app is compiled correctly, I think you should be able to have a decomposition of the drawBackground() method in the profiling information as well. It should then tell you which part of the method takes most time and it should be possible to obtain without recompiling Qt.
    ok finally I agree. You are right . I guess there is no decomposition because most method are inlined. Well I'll do that recompilation tomorrow(nice job in the weekend )
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: GraphicsView performance problems

    First do this (or something simmilar):
    Qt Code:
    1. void drawLines(QPainter *painter, QVarLengthArray<QLineF, 100> &lines){
    2. painter->drawLines(lines.data(), lines.size());
    3. }
    4.  
    5. void setupArray(QVarLengthArray<QLineF, 100> &lines){
    6. const int gridSize = 25;
    7. qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
    8. qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
    9. for (qreal x = left; x < rect.right(); x += gridSize)
    10. lines.append(QLineF(x, rect.top(), x, rect.bottom()));
    11. for (qreal y = top; y < rect.bottom(); y += gridSize)
    12. lines.append(QLineF(rect.left(), y, rect.right(), y));
    13. }
    14. void drawBackground(QPainter *painter, const QRectF &rect){
    15. painter->setPen(QPen(Qt::darkGreen,0));
    16. QVarLengthArray<QLineF, 100> lines;
    17. setupArray(lines);
    18. drawLines(painter, lines);
    19. }
    To copy to clipboard, switch view to plain text mode 

    This should allow you to measure how much time does it take to setup the array and how much to paint it.

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

    Default Re: GraphicsView performance problems

    Quote Originally Posted by wysota View Post
    First do this (or something simmilar):
    Qt Code:
    1. void drawLines(QPainter *painter, QVarLengthArray<QLineF, 100> &lines){
    2. painter->drawLines(lines.data(), lines.size());
    3. }
    4.  
    5. void setupArray(QVarLengthArray<QLineF, 100> &lines){
    6. const int gridSize = 25;
    7. qreal left = int(rect.left()) - (int(rect.left()) % gridSize);
    8. qreal top = int(rect.top()) - (int(rect.top()) % gridSize);
    9. for (qreal x = left; x < rect.right(); x += gridSize)
    10. lines.append(QLineF(x, rect.top(), x, rect.bottom()));
    11. for (qreal y = top; y < rect.bottom(); y += gridSize)
    12. lines.append(QLineF(rect.left(), y, rect.right(), y));
    13. }
    14. void drawBackground(QPainter *painter, const QRectF &rect){
    15. painter->setPen(QPen(Qt::darkGreen,0));
    16. QVarLengthArray<QLineF, 100> lines;
    17. setupArray(lines);
    18. drawLines(painter, lines);
    19. }
    To copy to clipboard, switch view to plain text mode 

    This should allow you to measure how much time does it take to setup the array and how much to paint it.
    Ok I did that, the profile now says
    Flat profile:

    Each sample counts as 0.01 seconds.
    % cumulative self self total
    time seconds seconds calls Ts/call Ts/call name
    50.05 0.02 0.02 Node:: paint(QPainter*, QStyleOptionGraphicsItem const*, QWidget*)
    25.03 0.03 0.01 GridScene::drawBackground(QPainter*, QRectF const&)
    25.03 0.04 0.01 Resistor::boundingRect() const
    0.00 0.04 0.00 192004 0.00 0.00 QVarLengthArray<QLineF, 100>::realloc(int, int)
    0.00 0.04 0.00 36506 0.00 0.00 GridScene::setupArray(QVarLengthArray<QLineF, 100>&, QRectF const&)
    0.00 0.04 0.00 36506 0.00 0.00 GridScene::drawLines(QPainter*, QVarLengthArray<QLineF, 100>&)
    0.00 0.04 0.00 1 0.00 0.00 global constructors keyed to _ZN9GridScene9drawLinesEP8QPainterR15QVarLengthArr ayI6QLineFLi100EE
    0.00 0.04 0.00 1 0.00 0.00 __static_initialization_and_destruction_0(int, int)
    Further

    Call graph (explanation follows)


    granularity: each sample hit covers 2 byte(s) for 24.97% of 0.04 seconds

    index % time self children called name
    <spontaneous>
    [1] 50.0 0.02 0.00 Node:: paint(QPainter*, QStyleOptionGraphicsItem const*, QWidget*) [1]
    -----------------------------------------------
    <spontaneous>
    [2] 25.0 0.01 0.00 GridScene::drawBackground(QPainter*, QRectF const&) [2]
    0.00 0.00 36506/36506 GridScene::setupArray(QVarLengthArray<QLineF, 100>&, QRectF const&) [12]
    0.00 0.00 36506/36506 GridScene::drawLines(QPainter*, QVarLengthArray<QLineF, 100>&) [13]
    -----------------------------------------------
    <spontaneous>
    [3] 25.0 0.01 0.00 Resistor::boundingRect() const [3]
    -----------------------------------------------
    0.00 0.00 192004/192004 GridScene::setupArray(QVarLengthArray<QLineF, 100>&, QRectF const&) [12]
    [11] 0.0 0.00 0.00 192004 QVarLengthArray<QLineF, 100>::realloc(int, int) [11]
    -----------------------------------------------
    0.00 0.00 36506/36506 GridScene::drawBackground(QPainter*, QRectF const&) [2]
    [12] 0.0 0.00 0.00 36506 GridScene::setupArray(QVarLengthArray<QLineF, 100>&, QRectF const&) [12]
    0.00 0.00 192004/192004 QVarLengthArray<QLineF, 100>::realloc(int, int) [11]
    -----------------------------------------------
    0.00 0.00 36506/36506 GridScene::drawBackground(QPainter*, QRectF const&) [2]
    [13] 0.0 0.00 0.00 36506 GridScene::drawLines(QPainter*, QVarLengthArray<QLineF, 100>&) [13]
    -----------------------------------------------
    0.00 0.00 1/1 __do_global_ctors_aux [25]
    [14] 0.0 0.00 0.00 1 global constructors keyed to _ZN9GridScene9drawLinesEP8QPainterR15QVarLengthArr ayI6QLineFLi100EE [14]
    0.00 0.00 1/1 __static_initialization_and_destruction_0(int, int) [15]
    -----------------------------------------------
    0.00 0.00 1/1 global constructors keyed to _ZN9GridScene9drawLinesEP8QPainterR15QVarLengthArr ayI6QLineFLi100EE [14]
    [15] 0.0 0.00 0.00 1 __static_initialization_and_destruction_0(int, int) [15]
    -----------------------------------------------
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  9. #8
    Join Date
    Jan 2006
    Posts
    128
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 28 Times in 27 Posts

    Default Re: GraphicsView performance problems

    Quote Originally Posted by Gopala Krishna View Post
    [2] 25.0 0.01 0.00 GridScene::drawBackground(QPainter*, QRectF const&) [2]
    0.00 0.00 36506/36506 GridScene::setupArray(QVarLengthArray<QLineF, 100>&, QRectF const&) [12]
    0.00 0.00 36506/36506 GridScene::drawLines(QPainter*, QVarLengthArray<QLineF, 100>&) [13]
    That looks really weird.
    The reason probably once again that the time is spent inside of Qt-Code and not yours, so sadly that does not help.
    Or it could have been inlined, again not really helpfull :-/


    Have you tried valgrind/callgrind and KCacheGrind as a profiler? It is rather nice :-)
    Last edited by camel; 17th February 2007 at 15:41.

  10. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: GraphicsView performance problems

    Quote Originally Posted by camel View Post
    That looks really weird.
    The reason probably once again that the time is spent inside of Qt-Code and not yours, so sadly that does not help.
    These figures are wrong. All functions from inside Qt should be counted on behalf of the functions calling them and here all functions have nothing but zeros. The application needs to run longer.

    Or it could have been inlined, again not really helpfull :-/
    Yes, that's possible, especially if they were implemented inside the class header and optimisations were enabled.

    You must be sure to disable inlining optimisations. You can use -fno-inline to disable inlining.

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

    Default Re: GraphicsView performance problems

    Yup! you both are right. Anyway I am recompiling Qt tomorrow. I'll surely post after I do that. In the meanwhile I'd like to inform you people that I have never used cachegrind. Is that better than gprof ? If yes how should I use that ?
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  12. #11
    Join Date
    Jan 2006
    Posts
    128
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 28 Times in 27 Posts

    Default Re: GraphicsView performance problems

    Did you try my last modification? Did it change anything for you?

    Quote Originally Posted by Gopala Krishna View Post
    Yup! you both are right. Anyway I am recompiling Qt tomorrow. I'll surely post after I do that. In the meanwhile I'd like to inform you people that I have never used cachegrind. Is that better than gprof ? If yes how should I use that ?
    You do not need to compile anything special (besides DEBUG of course ;-)

    Then call
    $valgrind --tool=callgrind ./YOUR_APP
    this will produce a file called callgrind.out.PID_OF_PROCESS

    then you can call
    $kcachegrind callgrind.out.PID_OF_PROCESS
    http://kcachegrind.sourceforge.net/cgi-bin/show.cgi

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

    Default Re: GraphicsView performance problems

    Quote Originally Posted by camel View Post
    Did you try my last modification? Did it change anything for you?



    You do not need to compile anything special (besides DEBUG of course ;-)

    Then call


    this will produce a file called callgrind.out.PID_OF_PROCESS

    then you can call


    http://kcachegrind.sourceforge.net/cgi-bin/show.cgi
    Thanks ! Here is the output as attachment
    EDIT: My quota of attachment is almost over. I'll update a bit later.
    Last edited by Gopala Krishna; 17th February 2007 at 16:34. Reason: updated contents
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: GraphicsView performance problems

    I have modified my application which uses QGraphicsView framework to draw a grid simmilar to yours and I implemented it in a worst possible way:
    Qt Code:
    1. void drawBackground ( QPainter * painter, const QRectF & rect ){
    2. static QPen p(QColor(150,150,150));
    3. painter->setPen(p);
    4. for(int row=10+((int)(rect.top())/10)*10; row<rect.bottom(); row+=10){
    5. for(int col = 10+((int)(rect.left())/10)*10; col<rect.right(); col+=10)
    6. painter->drawPoint(col, row);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    Profiling info (compiled with -fno-inline -ggdb -pg) looks like this:
    text Code:
    1. % cumulative self self total
    2. time seconds seconds calls us/call us/call name
    3. 33.33 0.02 0.02 1547664 0.01 0.01 QRectF::right() const
    4. 33.33 0.04 0.02 SpecScene::drawBackground(QPainter*, QRectF const&)
    5. 16.67 0.05 0.01 1527572 0.01 0.01 QPainter::drawPoint(int, int)
    To copy to clipboard, switch view to plain text mode 
    As you see 33% of the application time is spent in drawBackground and half of it is used to calculate QRectF::right() (which makes sense as it gets calculated in each iteration of the loop) and pretty much is used to actually draw the points.

    text Code:
    1. index % time self children called name
    2. <spontaneous>
    3. [1] 83.3 0.02 0.03 SpecScene::drawBackground(QPainter*, QRectF const&) [1]
    4. 0.02 0.00 1547627/1547664 QRectF::right() const [2]
    5. 0.01 0.00 1527572/1527572 QPainter::drawPoint(int, int) [3]
    6. 0.00 0.00 20635/20651 QRectF::bottom() const [35]
    7. 0.00 0.00 20055/20055 QRectF::left() const [36]
    8. 0.00 0.00 580/580 QRectF::top() const [93]
    9. 0.00 0.00 1/702 QColor::QColor(int, int, int, int) [92]
    To copy to clipboard, switch view to plain text mode 
    As you see it is actually more time consuming to calculate the rectangle coords than to draw points!

    Some facts:
    - I use Qt4.2.2 on i686 Linux,
    - I didn't use antialiasing,
    - I didn't use scaling (so I have an identity matrix when it comes to viewport-window transformations),
    - scene size was about 1200x1000,
    - I didn't compile Qt with profiling information (so I guess it's not required after all),
    - I got detailed info about both mine and Qt methods,
    - the result is pretty fast.

    I got a pretty good result, but I didn't suffer from viewport-window transformations which might be your case if you scale the view. I didn't use antialiasing for my view, as points are points - they don't suffer from the aliasing effect. I could easily improve the implementation by calculating rectangle coordinates once per drawBackground() which should reduce the execution effort by half.

    It is up to you to do the interpretation of the results. My impression is that it is not really drawBackground() which causes the slowdown - maybe it is just called too often by other parts of the system? Maybe you abuse update()?

  15. #14
    Join Date
    Jan 2006
    Posts
    128
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 28 Times in 27 Posts

    Default Re: GraphicsView performance problems

    Quote Originally Posted by wysota View Post
    I have modified my application which uses QGraphicsView framework to draw a grid simmilar to yours and I implemented it in a worst possible way:

    Profiling info (compiled with -fno-inline -ggdb -pg) looks like this:
    text Code:
    1. % cumulative self self total
    2. time seconds seconds calls us/call us/call name
    3. 33.33 0.02 0.02 1547664 0.01 0.01 QRectF::right() const
    4. 33.33 0.04 0.02 SpecScene::drawBackground(QPainter*, QRectF const&)
    5. 16.67 0.05 0.01 1527572 0.01 0.01 QPainter::drawPoint(int, int)
    To copy to clipboard, switch view to plain text mode 
    Well, if you disable inlining, no wonder right() is expensive. calling a function 1.5 Million times just takes its time...inlining is there for a reason ;-)

    So I would not call this run very exemplary... :-)

  16. #15
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: GraphicsView performance problems

    Hi

    I posted in the old thread too.

    Did you find a solution to draw the grid efficiently? I am running into the same problem. Also, when I zoomIn or zoomOut, my grid spacing is irregular.

    Kindly help me with suggestions.

    Thanks
    Arjun

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

    Default Re: GraphicsView performance problems

    I solved my problem by a change in representation of grids. Now i use points with no brush and pen of 0 width to represent grids instead of lines. This has given me significant improvement. Also i switched from qt4.2 to qt4.3 , which gave me freedom to optimize the view more. Setting the view's update mode to smart mode remarkably improved the performance and i no longer need to optimize it anymore

    As far as your problem is concerned can you give me some details of your application ? Some code or screenshots definitely help us understand your problem
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  18. The following user says thank you to Gopala Krishna for this useful post:

    arjunasd (8th August 2007)

  19. #17
    Join Date
    Jul 2007
    Posts
    39
    Thanks
    10

    Default Re: GraphicsView performance problems

    Hi

    I did the changes you suggested and the grid works fine now.

    I have another problem though. I have basic shapes like circle, ellipse, rectangle etc in my view and they are drawn with a penwidth of 2. When I zoomin, the rectangle is fine but the curves of circle and ellipse appear discontinuous.

    I know zooming will decrease the quality. But is there any way to overcome this and make the circle continuous.

    Thanks
    Arjun

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

    Default Re: GraphicsView performance problems

    Have you enabled antialiasing ?
    Qt Code:
    1. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  21. The following user says thank you to Gopala Krishna for this useful post:

    arjunasd (8th August 2007)

  22. #19
    Join Date
    Jan 2006
    Posts
    128
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 28 Times in 27 Posts

    Default Re: GraphicsView performance problems

    Well, I already did profile it globally and found something rather interesting.

    There is no use, trying to draw anything via the qpainter->draw(Rect, Elipsis, Polygons) whatever in Graphics view, since it is going to be converted to a painterpath anyways.


    That means, that for every paint, you create a new painterpath, which means that you need to allocate memory, which is slow.

    Why a painterpath?
    If you check the x11 paintengine, you see that as soon as antialiasing is enabled (or there is a certain amount of TxTranslate, whatever that means), drawing is done using the "path_fallback", which means primitives are first converted to paths before being drawn.
    Why? No clue ;-)

    But as a result that means, if you draw a lot of (rects, ellipses and polygons, lines...), use a painterpath instead. Preferably even one that you do not recalculate every time you are asked to draw something.. :-)


    The source after this transition:
    Qt Code:
    1. #include <QtGui>
    2. #include <cmath>
    3.  
    4. class GridScene : public QGraphicsScene
    5. {
    6. public:
    7. GridScene(qreal x, qreal y, qreal w, qreal h)
    8. : QGraphicsScene(x, y, w, h)
    9. { }
    10.  
    11. protected:
    12. void drawBackground(QPainter *painter, const QRectF &rect)
    13. {
    14. const int gridSize = 25;
    15.  
    16. const int realLeft = static_cast<int>(std::floor(rect.left()));
    17. const int realRight = static_cast<int>(std::ceil(rect.right()));
    18. const int realTop = static_cast<int>(std::floor(rect.top()));
    19. const int realBottom = static_cast<int>(std::ceil(rect.bottom()));
    20.  
    21.  
    22. const int firstLeftGridLine = realLeft - (realLeft % gridSize);
    23. const int firstTopGridLine = realTop - (realTop % gridSize);
    24.  
    25. QPainterPath background;
    26.  
    27. for (int x = firstLeftGridLine; x <= realRight; x += gridSize) {
    28. background.moveTo(x, realTop);
    29. background.lineTo(x, realBottom);
    30. background.closeSubpath();
    31. }
    32. for (int y = firstTopGridLine; y <= realBottom; y += gridSize) {
    33. background.moveTo(realLeft, y);
    34. background.lineTo(realRight, y);
    35. background.closeSubpath();
    36. }
    37.  
    38. //If you uncomment this, you will find the color problem lessened...
    39. //why? well your guess is as good as mine ;-)
    40. // painter->fillRect(QRect(QPoint(realLeft, realTop), QPoint(realRight, realBottom)), QBrush(Qt::white));
    41.  
    42. painter->setOpacity(1.0);
    43. painter->setPen(QPen(Qt::darkGreen,0));
    44. painter->drawPath(background);
    45. }
    46. };
    47.  
    48. namespace {
    49. static inline QPainterPath constructNodeShape(const QRect& elipseRect)
    50. {
    51. path.addEllipse(elipseRect);
    52. return path;
    53. }
    54. }
    55.  
    56. class Node : public QGraphicsItem
    57. {
    58. public:
    59. Node(QPointF pos ,QGraphicsItem *par = 0, QGraphicsScene *sc = 0)
    60. : QGraphicsItem(par,sc),
    61. elipseRect(-3, -3, 2*3, 2*3),
    62. elipseShape(constructNodeShape(elipseRect))
    63. {
    64. setPos(pos);
    65. setFlags(0);
    66. setAcceptedMouseButtons(0);
    67. }
    68.  
    69. void paint(QPainter* p,const QStyleOptionGraphicsItem *, QWidget *)
    70. {
    71. p->setPen(Qt::darkRed);
    72. p->setBrush(Qt::NoBrush);
    73. p->setOpacity(1.0);
    74.  
    75. p->drawPath(elipseShape);
    76. }
    77.  
    78. QPainterPath shape() const
    79. {
    80. return elipseShape;
    81. }
    82.  
    83. QRectF boundingRect() const
    84. {
    85. return elipseRect;
    86. }
    87.  
    88. protected:
    89. const QRect elipseRect;
    90. const QPainterPath elipseShape;
    91. };
    92.  
    93. class Resistor : public QGraphicsItem
    94. {
    95. public:
    96. Resistor(QGraphicsItem *par = 0, QGraphicsScene *scene = 0)
    97. : QGraphicsItem(par,scene)
    98. {
    99. setFlags(ItemIsMovable | ItemIsSelectable | ItemIsFocusable);
    100. // comment the following 4 lines to see the performance difference
    101. QGraphicsTextItem *t = new QGraphicsTextItem("R1 = 100k",this,scene);
    102. t->setPos(0,-35);
    103. new Node(QPointF(-30,0),this,scene);
    104. new Node(QPointF(30,0),this,scene);
    105.  
    106.  
    107. resistorPath.addRect(QRectF(-18.0, -9.0, 36.0, 18.0));
    108. resistorPath.moveTo(-27, 0);
    109. resistorPath.lineTo(-18, 0);
    110. resistorPath.closeSubpath();
    111. resistorPath.moveTo(18, 0);
    112. resistorPath.lineTo(27, 0);
    113. resistorPath.closeSubpath();
    114.  
    115. boundingBox = resistorPath.boundingRect();
    116. }
    117.  
    118. void paint(QPainter *p, const QStyleOptionGraphicsItem *o, QWidget *)
    119. {
    120. if(!(o->state & QStyle::State_Open))
    121. p->setPen(Qt::darkBlue);
    122. if(o->state & QStyle::State_Selected)
    123. p->setPen(QPen(Qt::darkGray,1));
    124.  
    125. p->setBrush(Qt::NoBrush);
    126. p->drawPath(resistorPath);
    127. }
    128.  
    129. QRectF boundingRect() const
    130. {
    131. return boundingBox;
    132. }
    133.  
    134. private:
    135. QPainterPath resistorPath;
    136. QRectF boundingBox;
    137. };
    138.  
    139. int main(int argc,char *argv[])
    140. {
    141. QApplication app(argc,argv);
    142. GridScene scene(0,0,1024,768);
    143. for(int j = 2; j < 4; ++j)
    144. for(int i = 1; i <11; ++i)
    145. {
    146. Resistor *r = new Resistor(0,&scene);
    147. r->setPos(j*100, i * 50);
    148. }
    149. QGraphicsView *view = new QGraphicsView(&scene);
    150. // Turn of antialiasing and the color problem is gone...
    151. view->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    152. view->setDragMode(QGraphicsView::RubberBandDrag);
    153. view->show();
    154. return app.exec();
    155. }
    To copy to clipboard, switch view to plain text mode 

  23. #20
    Join Date
    Jan 2006
    Posts
    128
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 28 Times in 27 Posts

    Default Re: GraphicsView performance problems

    Well...[CUT BECAUSE SOMETHING WEIRD IS GOING ON...it seems to be as if drawing the lines one by one is faster...but somehow that is rather unbelievable on second thought]...


    Another method of getting rid of spurious mallocs is to reuse Pens and Brushes...

    I guess i will not repost all of the code for these little changes...



    Does anyone has access to a profiler on windows/mac? I would like to know if those changes that were good for linux (ellipses, rect -> painterpath) lines drawn one by one, is also good for these palatforms ;-)
    Last edited by camel; 17th February 2007 at 16:14. Reason: wtf? second thoughts

Similar Threads

  1. Performance problems with overlapping qgraphicsitems
    By brjames in forum Qt Programming
    Replies: 13
    Last Post: 4th May 2008, 22:42
  2. QT GraphicsView Help
    By mistertoony in forum Qt Programming
    Replies: 15
    Last Post: 15th February 2007, 05:17
  3. Replies: 1
    Last Post: 4th October 2006, 17:05
  4. QT4 Plugins - problems, problems
    By NormanDunbar in forum Qt Programming
    Replies: 6
    Last Post: 9th May 2006, 16:39
  5. Increasing performance from Qtextedit, listview, etc?
    By taylor34 in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2006, 11: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
  •  
Qt is a trademark of The Qt Company.