Results 1 to 10 of 10

Thread: QPixmap in thread

  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QPixmap in thread

    Hello
    I want to convert QImage into QPixmap using QPixmap().convertFromImage(QImage) but the problem is that I have larg images, like 5k and that freezes Ui.

    Currently I convert images in another thread, and here is my question:

    is there any danger to create QPixamap in another thread if QPixmap is not accessed from non Ui thread - they are just created and stored in list?

    Is there any better way to do that? I need QPixmap and not QImage because I need to display them in QGraphicsScene.

    Regards
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  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: QPixmap in thread

    Yes, you can't create pixmaps in non-GUI threads. A potential solution is to tile the image so that you have smaller images that you can process in batches.
    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 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap in thread

    Technically you can, but you need to call XInitThreads, or pass AA_X11InitThreads to QApplication::setAttribute (). I did it and it worked... for a while
    On another machine it simply breaks, for the reason I haven't yet researched.
    So I made a fallback to QImage. I'm not very happy with performance though.
    If there's a way for doing deferred rendering in another thread I'd like to hear about that

  4. #4
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPixmap in thread

    Well I did manage to convert it in another thread and that works fine, excluding warning all over the place for the QPixmap in non GUI thread.

    QPixmap's are not thread safe so You can't use it in another thread.
    But my question was related not to the accessing issue but to the thread affinity for the QPixmap's. So if I create object in thread A then that object lives in thread A. For the i.e. QImage its fine, but I don't know if thread affinity will conflict with QPixmaps.

    AFAIK the reason is that only GUI thread has QPainterEngine active and that one is used to paint on QPixmaps. Depending on the server, i.e. X11, OGL etc. server strange thing can happens, especially on X11.

    PS. Tilling won't work, because total time will be, in average, the same. Because the reason why I need QPixmap is that I will display them, and there in no big time difference between converting 1 time 5kx5k QImage and 5 times 1kx1k QImage. Of course from speed conversion point of view, not Ui freezing, with using 5x 1kx1k conversion help.
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  5. #5
    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: QPixmap in thread

    Quote Originally Posted by Talei View Post
    But my question was related not to the accessing issue but to the thread affinity for the QPixmap's.
    QPixmap doesn't have any thread affinity. It's not a QObject. It simply relies on the platform dependent way to access graphical resources thus it has to be accessed from the UI thread. It may work for you today but it might as well fail tomorrow.

    PS. Tilling won't work, because total time will be, in average, the same.
    Total time will be the same but you can let your UI thread process events more frequently.
    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.


  6. #6
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QPixmap in thread

    QPixmap -> QObject, yes, I was thinking of other "affinity" but I don't know if it is correct anyway.

    So technically speaking if I just create QPixmap in another thread and don't display / access that qpixmap from non GUI thread, then It should always work? I mean why it shouldn't work, it's just created data in memory (on server side).

    I was thinking that if QPixmap will be created in different thread, and if I use i.e. OpenGL as render engine, then that QPixmap will live on server side in a memory assigned to that thread and If that thread will case to exist then that memory will be freed? But I think that's wrong assumption and explicit deletion is needed of such data.

    So the question is, can I create - and not use QPixmap - in non GUI thread and then only use it from main Ui thread? Or will that also produce "if'y" situation?
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  7. #7
    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: QPixmap in thread

    Quote Originally Posted by Talei View Post
    So technically speaking if I just create QPixmap in another thread and don't display / access that qpixmap from non GUI thread, then It should always work?
    I don't think there are any guarantees like that. "Creating" the pixmap means resource allocation which is the place where all can fail.

    I mean why it shouldn't work, it's just created data in memory (on server side).
    That's exactly why. Because the server connection is "owned" by a different thread.
    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.


  8. The following 2 users say thank you to wysota for this useful post:

    lanz (13th March 2013), Talei (12th March 2013)

  9. #8
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap in thread

    Well, that got me to think of interesting (or stupid ) solution.
    Maybe I can create another connection to server, thought it seems not a very cross-platform.
    Another variant maybe to create two processes tied by TCP/Dbus/pipes.
    I assuming of course that QPainter/QImage drawing is significantly slower than QPainter/QPixmap (latter using hardware acceleration), am I right?

  10. #9
    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: QPixmap in thread

    Quote Originally Posted by lanz View Post
    Well, that got me to think of interesting (or stupid ) solution.
    Maybe I can create another connection to server, thought it seems not a very cross-platform.
    As far as I understand how X11 works, there would be no way to use that pixmap using the other connection.

    I assuming of course that QPainter/QImage drawing is significantly slower than QPainter/QPixmap (latter using hardware acceleration), am I right?
    On X11 with real X11 drawing (and not the raster engine) you can only draw pixmaps (an image is converted to a pixmap before being drawn). With the raster engine (e.g. on Windows) I think it doesn't matter whether you use QPixmap or QImage. I'm not sure of that though.
    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.


  11. #10
    Join Date
    Dec 2012
    Posts
    90
    Thanks
    5
    Thanked 20 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QPixmap in thread

    Quote Originally Posted by wysota View Post
    As far as I understand how X11 works, there would be no way to use that pixmap using the other connection.
    Yeah... I thought of speeding up painter draw calls, but seems like it cancels out by converting from pixmap at one connection, then recreating pixmap at the other.

Similar Threads

  1. Replies: 1
    Last Post: 25th October 2012, 15:10
  2. QPixmap: It is not safe to use pixmaps outside the GUI thread
    By bibhukalyana in forum Qt Programming
    Replies: 9
    Last Post: 16th May 2011, 07:30
  3. Replies: 4
    Last Post: 28th August 2008, 13:13
  4. Replies: 1
    Last Post: 21st August 2008, 07:44
  5. Replies: 5
    Last Post: 9th April 2007, 14:26

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.