Results 1 to 5 of 5

Thread: CPU intensive QPainter

  1. #1
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default CPU intensive QPainter

    Hi all,

    I am new to QT, and for reference am using QT4.
    I intend to draw points in an image of 255 x 255 in realtime.

    To draw the points (number of points variable), I do:
    Qt Code:
    1. QColor b(0,0,255,255);
    2. QPainter p(&pixmap);
    3. p.setPen(b);
    4. p.drawPoint(y, x);
    5. p.drawPoint(y, x);
    6. p.drawPoint(y, x);
    7. p.drawPoint(y, x);
    8. p.drawPoint(y, x);
    9. ...
    10. p.drawPoint(y, x);
    11. p.drawPoint(y, x);
    To copy to clipboard, switch view to plain text mode 


    I am using a label to show the image where I just do
    Qt Code:
    1. label->setPixmap(pixmap)
    To copy to clipboard, switch view to plain text mode 

    Every 40ms I erase the image to restart drawing new image of points with:
    Qt Code:
    1. QPainter p(pixmap);
    2. p.eraseRect(0,0, 255, 255);
    To copy to clipboard, switch view to plain text mode 

    Everything is working fine, but...
    First, I would like to know if this is the correct way of creating/showing image of points or if there is a better approach.
    Second, this appears to be very CPU intensive, is there a problem with this code? Should I be using something different instead of QPainter and pixmap? I get up to 80% CPU usage on core2duo 2,53GHz. Thus unacceptable for such a simple program.
    Third, intend to do this for maybe 3 or more images at the same time so not only is unacceptable but becomes impossible...

    Thank you in advance for any help,

    Regards

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

    Default Re: CPU intensive QPainter

    So how many points are you drawing in one go?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPU intensive QPainter

    Hi,

    The number of points is variable, theoretically would be between 1 and all (255 x 255 = 65025)
    But I guess I never even do more than 200.

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

    Default Re: CPU intensive QPainter

    Try using QPainter::drawPoints(), it should be much faster than calling drawPoint() a number of times. if it's still not enough, perform drawing on QImage instead of QPixmap but not using the painter API but rather by retrieving the pixel data (QImage::bits()) and manipulating the pixels directly. Then convert the image back to a pixmap. You can even manipulate the image in a separate thread if your machine is multicore. Also consider limiting your frame rate - if you erase the pixmap every 40ms it means you are doing that 25 times per second which is more than your eye can actually understand. You can halve the rate without making the user experience worse.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    jcarneiro (20th October 2010)

  6. #5
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: CPU intensive QPainter

    Thanks a lot,

    Indeed using drawpoints method is extremely faster.
    Wanted to try the other approach but didn't quite manage to do it, seems like I'm getting some random data.
    To show the image I am doing:
    Qt Code:
    1. QImage tmp(Imagebits, 255, 255, QImage::Format_Mono);
    2. QPixmap pixMap = QPixmap::fromImage(tmp);
    3. ui->label->setPixmap(pixMap);
    To copy to clipboard, switch view to plain text mode 

    to clear the image I am using:
    Qt Code:
    1. QImage tmp(Imagebits, 255, 255, QImage::Format_Mono);
    2. tmp.fill(0);
    3. Imagebits = tmp.bits();
    To copy to clipboard, switch view to plain text mode 

    without changing any bit in the array I am already getting random data.
    By the way, thinking on how to access the array, method bits returns an uchar* does. It allocate a fixed size for all format or it is format dependent and in case mono pixels 0 - 7 are in first position etc?

    Regarding the frame rate/erase redraw frequency it's true and a very good idea although I am acquiring data much faster and drawing real-time into pixmap (the 40 ms was more for context ). Even if I don't draw anything onscreen, just writing with drawpoint onto pixmap is very slow.

Similar Threads

  1. QPainter(&QPrinter) & QPainter(&QImage) communication
    By gufeatza in forum Qt Programming
    Replies: 2
    Last Post: 2nd February 2010, 07:25
  2. Replies: 5
    Last Post: 7th September 2009, 20:57
  3. QPainter
    By Misko in forum Newbie
    Replies: 9
    Last Post: 5th August 2007, 20:23
  4. QPainter::save and QPAinter::restore()
    By quickNitin in forum Newbie
    Replies: 2
    Last Post: 17th June 2006, 22:11
  5. Replies: 3
    Last Post: 30th April 2006, 19:22

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.