View Poll Results: Which is better one?

Voters
15. You may not vote on this poll
  • OpenGl

    6 40.00%
  • QPainter

    9 60.00%
Results 1 to 6 of 6

Thread: QPainter update()

  1. #1
    Join Date
    Mar 2008
    Posts
    68
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QPainter update()

    Hello There,

    i am developing an application to display 2D objects on screen with Qt4.2.2

    i am using QPainter class to show objects on the screen.
    The Requirement is to show small ellipse like objects. The Total number of objects is 500 and all these should be updated on the screen in micro seconds.

    i find the QPainter Class taking time of about 200 milliseconds on average to update atleast 300 plots.i use update() method to avoid flicker.

    should i need to use repaint class to update the screen()?if yes then how shall i avoid flicker.

    is there any other method other than repaint() or update() to display the objects?
    should i need to use QPainter Class or is there any other option to update the screen in microseconds?

    Thanks in Advance...

    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    3. timer->start(1);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void AnalogClock::paintEvent(QPaintEvent *)
    2. {
    3.  
    4. int side = qMin(width(), height());
    5. static int i = 0, j =0;
    6. static int flag = 1;
    7. int diameter;
    8. if(j<420)
    9. {
    10. j++;
    11. }
    12. else
    13. {
    14. printf("\nThe cleint exited");
    15. exit(0);
    16. }
    17. QPainter painter(this);
    18. painter.setRenderHint(QPainter::Antialiasing);
    19. side =qMin(width(),height() );
    20. painter.translate(side / 2, side / 2);
    21.  
    22. painter.drawRect(-side/2,-side/2,side,side);
    23.  
    24. painter.fillRect(QRect(-side/2,-side/2,side,side),QBrush(Qt::black));
    25.  
    26. static const QPointF points[4] = {
    27. QPointF(0,-side/2),
    28. QPointF(0,side/2),
    29. QPointF(-side/2,0),
    30. QPointF(side/2,0)
    31. };
    32. painter.setPen(QPen(Qt::green,1,Qt::DotLine));
    33. painter.drawLines(points,2);
    34. painter.setPen(QPen(Qt::green));
    35. painter.drawText(420,0,"60.0");
    36. painter.drawText(350,0,"50.0");
    37. painter.drawText(280,0,"40.0");
    38. painter.drawText(210,0,"30.0");
    39. painter.drawText(140,0,"20.0");
    40. painter.drawText(70,0,"10.0");
    41. painter.save();
    42. painter.restore();
    43. painter.setPen(QPen(Qt::green));
    44. for (diameter = 140; diameter<=840; diameter+=140)
    45. { painter.drawEllipse(QRect(-diameter / 2, -diameter / 2,
    46. diameter, diameter));
    47. }
    48. painter.setPen(QPen(Qt::black));
    49. for(i = 1; i <= 420; i= i+5)
    50. {
    51. if(flag == 1)
    52. {
    53. painter.setBrush(QBrush(Qt::red,Qt::SolidPattern));
    54. flag = 0;
    55. }
    56. else
    57. {
    58. painter.setBrush(QBrush(Qt::blue,Qt::SolidPattern));
    59. flag = 1;
    60. }
    61. painter.drawEllipse(QRect(i,j,10,10));
    62. painter.drawEllipse(QRect(-i,j,10,10));
    63. painter.drawEllipse(QRect(i,-j,10,10));
    64. painter.drawEllipse(QRect(-i,-j,10,10));
    65. }
    66. painter.end();
    67. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st March 2008 at 09:43. Reason: reformatted to look better

  2. #2
    Join Date
    Mar 2008
    Posts
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QPainter update()

    Instead of painting in widget ..........
    try to paint in Qpixmap .....
    then use bitBlt() function to decrease flickerring

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

    Default Re: QPainter update()

    Quote Originally Posted by csvivek View Post
    and all these should be updated on the screen in micro seconds.
    Why? Be aware that if you have a 2GHz CPU, it can execute not more (which in most cases means "less") than 2G machine instructions per second. If you want to update contents within microseconds (10e-6) bounds, you are leaving only a few thousand cycles for such an update and all other operations performed by the machine including context switching (that by itself takes about 2-5 thousand cycles per switch). So there is no way you're going to update 300 plots like that.

    i use update() method to avoid flicker.
    No, you use update() to ask the widget to repaint itself.

    should i need to use repaint class to update the screen()?if yes then how shall i avoid flicker.
    What do you mean by "repaint class"?

    is there any other method other than repaint() or update() to display the objects?
    Simplifying things, repaint() calls paintEvent() directly and update() performs a delayed call to paintEvent(), they don't perform updates themselves. Anyway avoid using repaint() unless you know you are smarter than Qt in a particular situation. In 99% of the cases you'll be using update().

    should i need to use QPainter Class or is there any other option to update the screen in microseconds?
    There is no option unless you can use your GPU to render the plot and reduce the load on CPU. And certainly not using QTimer, as it operates with milisecond frequency.

    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    3. timer->start(1);
    To copy to clipboard, switch view to plain text mode 
    Do you really need 1000 updates per second? You wouldn't even notice subsequent changes, OpenGL or not. Human eye can see fluent motion with 16 frames per second, so if you change "1" to "50" to get 20 fps, you should get huge improvement without quality loss.

    Quote Originally Posted by Manohar View Post
    Instead of painting in widget ..........
    try to paint in Qpixmap .....
    then use bitBlt() function to decrease flickerring
    bitBlt() is no longer used in Qt4, all widgets are by default double buffered. Besides rendering to a pixmap in that case is pointless, because the contents change very frequently, so the application would render the contents to the pixmap and then blit it to the widget only once, so it's quicker to render directly. Using a pixmap could be benefitial if the contents were static.

    Cheers!

  4. #4
    Join Date
    Feb 2008
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter update()

    Use QGraphicsView instead, this is one of the things it's meant to be used for

    Also, OpenGL is not a magic option to speed things up (especially these kind of things), please keep that in mind.

  5. #5
    Join Date
    Mar 2008
    Posts
    68
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QPainter update()

    Hi

    Thnx for the reply...

    I am new to QT and have no information about QGraphicsView, i just read through the concept, i have time constraints to implemtent my requirement.

    My requirement is i need to receive data from a socket, to be continuously drawn on the widget.
    The socket is pumping data with the worst case speed of 1ms.

    I have a thread to receive the data, after reception i copy the data into the widget's member to be drawn, then i call the update() function.

    here i have two problems
    1.Update requires some time to finish its job, but by this time there is a possibility of receiving new data.
    here i may miss soem data from socket or painting for that set is incomplete ...

    2.For the sake of optimization, its said that "Calling update() several times normally results in just one paintEvent() call." so in this case what happens to the data which is to be drawn for each update?
    i.e. lets say i received 4 data which would result in calling 4 updates and ideally the widget was supposed to be redrawn 4 times.
    but due to this optimization i may have only the last data read.

    Can anybody suggest what needs to be done for this, i think i need to implement a buffer, but i dont know how to pass this info for PaintEvent.

    Now does QGraphicsView solve my problem?

  6. #6
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPainter update()

    Quote Originally Posted by csvivek View Post
    My requirement is i need to receive data from a socket, to be continuously drawn on the widget.
    Continuity is a purely mathematical concept. Software is limited by the hardware it runs on and is thus, by essence, discrete...

    Now you need to make a choice :
    • is your plot meant to be viewed in real time by the user with no possibility to look backward? Then skip some paint events, possibly caching the data if it does not overlap.
    • if this is not the case store everything and render on demand
    As a side note you might consider updating only the part of your widget which need to be updated.

    I also just wanted to point out the QPainter vs OpenGL does not make sense since QPainter can use OpenGL... Just use a QGLWidget as a viewport and QPainter will rely on OpenGL for all its drawing operations... That's the point of this class by the way : abstracting the drawing operations.
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. immediate update of QT object
    By db in forum Newbie
    Replies: 3
    Last Post: 14th August 2007, 13:25
  2. why there is no update on maximize? [QTableWidget]
    By macias in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2007, 16:01
  3. How to update statusbar and tooltips
    By aamer4yu in forum Qt Programming
    Replies: 9
    Last Post: 21st December 2006, 13:38
  4. QSlider Update Problem
    By December in forum Qt Programming
    Replies: 4
    Last Post: 10th September 2006, 05:02
  5. Replies: 7
    Last Post: 20th March 2006, 21:03

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.