[PyQt] Re: Segfaults on MacOS

Michael Held michael.held at bc.biol.ethz.ch
Sun Jun 28 22:52:22 BST 2009


ok, maybe my debugging is helpful and I am really PyQt newbie, but a
mutex helped the seg fault.
I guess this is some kind of concurrency issue of QImage.pixel

so my new version runs stable (although I am not sure if am really
supposed to an mutex myself ;-) )

cheers,
michael


class ImageViewer(QScrollArea):

    def __init__(self, parent):
        super(ImageViewer, self).__init__(parent)
        self.setBackgroundRole(QPalette.Dark)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        self.label = QLabel(self)
        self.label.setBackgroundRole(QPalette.Base)
        self.setAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
        self.label.installEventFilter(self)
        self.label.show()
        self.label.setMouseTracking(True)
        self.setWidget(self.label)
        self._qimage = None
        self.connect(self, SIGNAL('MouseMovedOverImage'),
                     self._on_move)
        self._mutex = mutex.mutex()

    def from_numpy(self, data):
        self._qimage = numpy_to_qimage(data)
        self._update()

    def from_qimage(self, qimage):
        self._qimage = qimage
        self._update()

    def _on_move(self, pos):
        if self._mutex.testandset():
            print pos, self._qimage.pixel(pos)
            self._mutex.unlock()


    def eventFilter(self, obj, ev):
        if ev.type() == QEvent.MouseMove and obj == self.label:
            self.emit(SIGNAL('MouseMovedOverImage'),
                      ev.pos())
            return True
        else:
            return super(ImageViewer, self).eventFilter(obj, ev)

    def _update(self):
        self.label.setPixmap(QPixmap.fromImage(self._qimage))
        self.label.resize(self.label.pixmap().size())
        #self.setMaximumSize(self.label.pixmap().size()+QSize(15,15))


Michael Held wrote:
> hi,
> 
> I am building an image viewer with PyQt and wanted to display the pixel
> color under the cursor for mouseMoveEvent - everything fine so far.
> using the eventFilter I was able circumvent QLabel sub-classing.
> 
> *BUT* printing the pixel value crashes my program with a seg fault (I am
> just moving a bit over the image and BANG...)
> my silly attempt to delay the event-response with time.sleep(0.5) did
> also not work.
> 
> I was using Stackless Python 2.6.2 before but the same error happened
> for the normal Python 2.6.2
> MacOS 10.5.7
> PyQt-mac-gpl-4.5.2-snapshot-20090627
> sip-4.8.1
> 
> thanks a lot!
> michael
> 
> 
> class ImageViewer(QScrollArea):
> 
>     def __init__(self, parent):
>         super(ImageViewer, self).__init__(parent)
>         self.setBackgroundRole(QPalette.Dark)
>         self.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
>         self.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
>         self.label = QLabel(self)
>         self.label.setBackgroundRole(QPalette.Base)
>         self.setAlignment(Qt.AlignHCenter|Qt.AlignVCenter)
>         self.label.installEventFilter(self)
>         self.label.show()
>         self.label.setMouseTracking(True)
>         self.setWidget(self.label)
>         self._qimage = None
>         self.connect(self, SIGNAL('MouseMovedOverImage'),
>                      self._on_move)
> 
>     def from_numpy(self, data):
>         self._qimage = numpy_to_qimage(data)
>         self._update()
> 
>     def from_qimage(self, qimage):
>         self._qimage = qimage
>         self._update()
> 
>     def _on_move(self, pos):
>         print pos, self._qimage.pixel(pos)
>         time.sleep(0.5)
> 
>     def eventFilter(self, obj, ev):
>         if ev.type() == QEvent.MouseMove and obj == self.label:
>             self.emit(SIGNAL('MouseMovedOverImage'),
>                       ev.pos())
>             return True
>         else:
>             return False
> 
>     def _update(self):
>         self.label.setPixmap(QPixmap.fromImage(self._qimage))
>         self.label.resize(self.label.pixmap().size())
> 



More information about the PyQt mailing list