PyQt4 - widget signal trouble

Marco Bizzarri marco.bizzarri at gmail.com
Sat Apr 25 17:47:57 EDT 2009


On Fri, Apr 24, 2009 at 7:37 PM, Joacim Thomassen
<joacim at net.homelinux.org> wrote:
> Hello,
>
> I'm trying to get my first PyQt4 application to work as intended, but it
> seems I'm stuck and out of ideas for now.
>
> The program is a simple GUI showing an image. If the image on disk change
> my intension is that the displayed image in my application also change
> accordingly.
>
> What works: The filesystem change is detected and my program prints out
> "Change happened!"
>
> what is the problem: The image shown in the application is not changed.
>
> What am I doing wrong here? Any ideas and suggestions are appreciated.
>
> Best regards,
> Joacim Thomassen
>
> My program:
> #!/usr/bin/python
> """
> familyframe.py
>
> Simple photo frame for the desktop
>
> Author: Joacim Thomassen, 4/2-2009
> License: AGPLv3+
>
> Last change: 24/2-2009
> """
>
> from __future__ import division
> import sys
> from math import *
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> import time
> import fcntl
> import os
> import signal
>
> fname = "/home/joacim/.familyframe"
>
> class Watcher(QObject):
>        def handler(self, signum, frame):
>                self.emit(SIGNAL("imageChange"))
>        def __init__(self, parent=None):
>                super(Watcher, self).__init__()
>                signal.signal(signal.SIGIO, self.handler)
>                fd = os.open(fname, os.O_RDONLY)
>                fcntl.fcntl(fd, fcntl.F_SETSIG, 0)
>                fcntl.fcntl(fd, fcntl.F_NOTIFY, fcntl.DN_MODIFY |
> fcntl.DN_CREATE | fcntl.DN_MULTISHOT)
>
> class ImageWidget(QLabel):
>        def __init__(self, parent=None):
>                super(QLabel, self).__init__(parent)
>                self.image = QImage("/home/joacim/.familyframe/image.jpg")
>                self.setMinimumSize(200, 200)
>                self.setAlignment(Qt.AlignCenter)
>                self.setPixmap(QPixmap.fromImage(self.image))
>        def reload(self):
>                print "Change happened!"
>                self.image.load("/home/joacim/.familyframe/image.jpg")
>                self.setPixmap(QPixmap.fromImage(self.image))
>                self.update()
>
> class CentralWidget(QWidget):
>        def __init__(self, parent=None):
>                super(QWidget, self).__init__(parent)
>                self.imagewidget = ImageWidget()
>                self.box = QHBoxLayout()
>                self.box.addWidget(self.imagewidget)
>                self.setLayout(self.box)
>        def reload(self):
>                self.imagewidget.reload()
>
> class MainWindow(QMainWindow):
>        def __init__(self, w, parent=None):
>                super(MainWindow, self).__init__(parent)
>                self.centralwidget = CentralWidget()
>                self.setWindowTitle("Family Frame")
>                self.setCentralWidget(self.centralwidget)
>                self.connect(w, SIGNAL("imageChange"), self.updateUi)
>                self.show()
>        def updateUi(self):
>                self.centralwidget.reload()
>
> if __name__ == "__main__":
>        app = QApplication(sys.argv)
>        w = Watcher()
>        main = MainWindow(w)
>        app.exec_()
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Ciao, Joacim.

Too much since I 'played' with low level calls, so I may be wrong. But
it seems to me you're opening the image and monitoring for changes the
directory. Hence the problem. If you read/write a file, I think the
directory does not result as modified (but you should experiment by
yourself).

Regards
Marco


-- 
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/



More information about the Python-list mailing list