Results 1 to 8 of 8

Thread: QImage and threads

  1. #1
    Join Date
    Dec 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default QImage and threads

    Hi,

    I'm working on a raytracer. As that's a very cpu-heavy process, I'd like to run the raytracer itself in a seperate thread. Is it possible to write from the thread directly to a QImage (which is displayed in the gui), so you can see the picture being created? Or isn't that safe and do I have to pass the new pixel data via signals/slots to the gui thread?

    And how do I display the QImage? With a QLabel (or is that only for QPixmap)

    Thanks in advance for helping out a newbie =)

  2. #2
    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
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QImage and threads

    You can write to an image in a separate thread but you can't show it on the screen because it has to be converted to a pixmap first. But you can do calculations in the worker thead and every time a scanline is complete you can send it to the main thread (using signals and slots), assemble the image, convert it to pixmap and display on the screen (or better yet hold each scanline as a separate pixmap to avoid needless conversions from image to pixmap).
    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
    Dec 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QImage and threads

    Thanks for your answer, sorry for my late reply :S

    So I write in my worker thread each scanline to a qpixmap (size for instace 1024x1 pixel), then I send the qpixmap using a slot to the gui thread, which adds the qpixmap to the whole image?

    And with what method of QPixmap can I add an other pixmap to the image. Or do I just have to use fill for every pixel?

  4. #4
    Join Date
    Dec 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QImage and threads

    I've written a little test application for it. The worker thread emits a signal with a QImage containing a row of the image. This signal is connected to this slot:

    Qt Code:
    1. void DisplayImage::newData(int row, QImage newData)
    2. {
    3. int y = pixmap->height() - 1 - row; //if height is 400, y is in 0-399
    4. //In the raytracer 0,0 is the bottomleft of the screen, not the topleft
    5. painter.drawImage(0, y, newData);
    6. displayLabel.setPixmap(*pixmap);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Is this the fastest way to do it, or can I optimize this?

  5. #5
    Join Date
    Dec 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QImage and threads

    I did some other test, and this works correctly, but it has a huge impact on the speed. When create a picture without setting the new QPixmap to the label, it renders much faster.

    Qt Code:
    1. //displayLabel->setPixmap(displayPixmap);
    To copy to clipboard, switch view to plain text mode 

    (with this line commented out)

    Does anybody know if there is a faster way to display the QPixmap or how to optimize this ?

  6. #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
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QImage and threads

    Don't use a label. Use a custom widget and store each scanline as a separate pixmap as I suggested earlier.
    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.


  7. #7
    Join Date
    Dec 2007
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2

    Default Re: QImage and threads

    Quote Originally Posted by wysota View Post
    Don't use a label. Use a custom widget and store each scanline as a separate pixmap as I suggested earlier.
    Thanks for your answer. So I create a new Widget class, reimplement paintEvent and let the painter paint my scanlines on the widget?

  8. #8
    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
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QImage and threads

    Yes. And only paint those scanlines that need painting (observe the rectangle you get in the paint event).
    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.


Similar Threads

  1. Replies: 12
    Last Post: 7th September 2011, 16:37
  2. QThread trying to limit threads
    By spawn9997 in forum Qt Programming
    Replies: 2
    Last Post: 2nd September 2009, 15:51
  3. Threads and QwtPainter::drawText
    By AwDogsgo2Heaven in forum Qwt
    Replies: 1
    Last Post: 15th July 2009, 16:45

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.