Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Why does instantiating QPixmap in a second thread block/slow main?
Forum Updated to NodeBB v4.3 + New Features

Why does instantiating QPixmap in a second thread block/slow main?

Scheduled Pinned Locked Moved Solved Qt for Python
3 Posts 2 Posters 767 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.
  • N Offline
    N Offline
    nobru
    wrote on last edited by
    #1

    While a secondary thread is instantiating a big (32 megapixels) image, the main is blocked/slow.
    In this example code, main responsiveness is tested by logging mouseMoveEvent.
    Left-click starts a second thread which instantiates QPixmap. While this is happening, only a few mouseMoveEvent are logged.
    Right-click starts a cpu-bound thread. While the cpu-bound thread is running, many mouseMoveEvent are logged.
    test log on pastebin.com
    Why does this happen? How can I avoid it?

    import time
    import threading
    import sys
    
    from PyQt5 import Qt, QtCore
    
    def instantiate_qpixmap():
        pic_data = open('32megapixels.jpg', 'rb').read()
        print('{} - START QPIXMAP (ident {})'.format(time.time(), threading.get_ident()))
        for i in range(25):
            Qt.QPixmap().loadFromData(pic_data)
        print('{} - END QPIXMAP'.format(time.time()))
    
    def cpu_bound_work():
        print('{} - START CPU-BOUND (ident {})'.format(time.time(), threading.get_ident()))
        for i in range(100000000):
            a = i + 1
        print('{} - END CPU-BOUND'.format(time.time()))
    
    
    class widget(Qt.QWidget):
        
        def mouseMoveEvent(self, event):
            print('{} - pos {}'.format(time.time(), event.screenPos()))
    
        def mousePressEvent(self, event):
            if event.buttons() == QtCore.Qt.LeftButton:
                print('{} - LEFT (ident {})'.format(time.time(), threading.get_ident()))
                threading.Thread(target=instantiate_qpixmap).start()
                print('{} - LEFT launched'.format(time.time()))
            else:
                print('{} - RIGHT (ident {})'.format(time.time(), threading.get_ident()))
                threading.Thread(target=cpu_bound_work).start()
                print('{} - RIGHT launched'.format(time.time()))
    
    
    app = Qt.QApplication(sys.argv)
    
    wid = widget()
    wid.show()
    
    sys.exit(app.exec_())
    
    1 Reply Last reply
    0
    • N Offline
      N Offline
      nobru
      wrote on last edited by
      #2

      About "no QGui outside main", I have indeed found people saying that e.g. creating QPixmaps in a thread or It is not safe to use QPixmap outside the gui Thread
      Where is the documentation about this? I don't even get the warning.
      How do I speed up my software? I suppose instantiating a QPixmap does the heavy jpg -> raster transformation which I suppose is slowing down my software. Would it be possible to do the transformation in another object (QImage?) in a secondary thread?

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nobru
        wrote on last edited by
        #3

        The documentation is here Threads and QObjects
        «Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.»
        I suppose QPaintDevice (inherite by both QPixmap and QImage) is one of the "GUI classes".
        So maybe I could do the jpg->raster transformation with something non-GUI (non-Qt) and pass the data to main.

        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