Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Not safe to use QPixmap outside the GUI thread
QtWS25 Last Chance

Not safe to use QPixmap outside the GUI thread

Scheduled Pinned Locked Moved General and Desktop
qpixmap
4 Posts 3 Posters 4.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    Yeah, I've seen threads about this. Dozens. They are all pretty short, and end with "don't do this - use QImage".

    Fine. However...

    I investigate such a case. An interestingly, although the warning is displayed, I haven't seen any bad effects. With dozens of users causing the warning for the past few weeks. Both on Windows and Linux. And nobody misses any pixmaps, so they are all there.

    So I wonder if the warning is indeed still legit. Of course, I wouldn't try to paint a pixmap in any other thread but the GUI thread. But reading it from file, and storing it in an object? It seems to work just fine.

    So could someone shed more light on what really happens to a QPixmap outside the GUI thread, and why it's not a good idea to use it there?

    1 Reply Last reply
    1
    • JKSHJ Offline
      JKSHJ Offline
      JKSH
      Moderators
      wrote on last edited by
      #2

      Hi,

      @Asperamanca said:

      I haven't seen any bad effects. With dozens of users causing the warning for the past few weeks. Both on Windows and Linux. And nobody misses any pixmaps, so they are all there.

      That's called "undefined behaviour". It works in the cases you've observed, but the designers have not made any attempt to guarantee that it will always work. The designers might change the internals in the future in a way that causes it to stop working.

      So I wonder if the warning is indeed still legit. Of course, I wouldn't try to paint a pixmap in any other thread but the GUI thread. But reading it from file, and storing it in an object? It seems to work just fine.

      So could someone shed more light on what really happens to a QPixmap outside the GUI thread, and why it's not a good idea to use it there?

      Interesting question. I recommend asking at the Interest mailing list, where the Qt engineers hang out.

      If you're feeling adventurous, have a look through the QPixmap source code: http://code.woboq.org/qt5/qtbase/src/gui/image/qpixmap.h.html (it's interactive -- clicking a function in the header will take you to its implementation, for example)

      One thing I noticed is that QPixmap objects read/write global objects without mutexes or any other thread locking.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      A 1 Reply Last reply
      1
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #3

        As stated in the docs and in contrast to QImage, QPixmap has an underlying platform specific storage implementation. So whether or not it's safe to use QPixmap in a worker thread partially depends on that platform specific underlying type. It might be the case that on some platforms you can't move that type between threads easily. It might also well be that most operations are safe in your particular case, but something like detach(), where the data needs to be copied, might not be thread safe or unsupported between threads on some platforms.

        My guess is some of the operations could be assured to work. It's just that the code is playing it safe. I'm not surprised by that knowing the multitude of platforms supported and the fact that even on one platform the backends may vary (e.g. the experiments with OpenGL backend in Qt 4 where making textures thread safe and copyable could be problematic).

        But as JKSH said you might get more gritty details from the devs.

        1 Reply Last reply
        1
        • JKSHJ JKSH

          Hi,

          @Asperamanca said:

          I haven't seen any bad effects. With dozens of users causing the warning for the past few weeks. Both on Windows and Linux. And nobody misses any pixmaps, so they are all there.

          That's called "undefined behaviour". It works in the cases you've observed, but the designers have not made any attempt to guarantee that it will always work. The designers might change the internals in the future in a way that causes it to stop working.

          So I wonder if the warning is indeed still legit. Of course, I wouldn't try to paint a pixmap in any other thread but the GUI thread. But reading it from file, and storing it in an object? It seems to work just fine.

          So could someone shed more light on what really happens to a QPixmap outside the GUI thread, and why it's not a good idea to use it there?

          Interesting question. I recommend asking at the Interest mailing list, where the Qt engineers hang out.

          If you're feeling adventurous, have a look through the QPixmap source code: http://code.woboq.org/qt5/qtbase/src/gui/image/qpixmap.h.html (it's interactive -- clicking a function in the header will take you to its implementation, for example)

          One thing I noticed is that QPixmap objects read/write global objects without mutexes or any other thread locking.

          A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          @JKSH

          @JKSH said:

          One thing I noticed is that QPixmap objects read/write global objects without mutexes or any other thread locking.

          That's one good argument right there. Currently, the application never tries to work on the QPixmaps from different threads at the same time.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved