Re: PyQt4 - widget signal trouble

Discussion in 'Python' started by Marco Bizzarri, Apr 25, 2009.

  1. On Fri, Apr 24, 2009 at 7:37 PM, Joacim Thomassen
    <> 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/
    Marco Bizzarri, Apr 25, 2009
    #1
    1. Advertising

  2. On Sun, Apr 26, 2009 at 12:38 PM, Joacim Thomassen
    <> wrote:
    > Den Sat, 25 Apr 2009 23:47:57 +0200, skrev Marco Bizzarri:
    >
    >
    > Hello Marco,
    >
    > python's fcntl() call the regular C fcntl() function and as stated in the
    > manual pages for C fcntl:
    >
    > ----------- Snippet from fcntl man pages ----------------
    >
    > File and directory change notification (dnotify)
    >       F_NOTIFY (long)
    >              (Linux  2.4 onwards) Provide notification when the
    >                      directory referred to by fd or any of the files that it
    >                      contains is changed.  The  events  to  be  notified  are
    >              specified in arg, which is a bit mask specified by ORing
    >                      together zero or more of the following bits:
    >
    >              DN_MODIFY   A file was modified (write, pwrite, writev,
    >                                                       truncate, ftruncate).
    > ------------- End snippet of fcntl man pages -------------------
    >
    > The fact that my program actually trigger a signal as the monitored
    > directoy's image.jpg file change confirm that this part of the code do
    > work. I do get "Change happened!" as i manually do a "cp another.jpg
    > image.jpg", but this action is first seen after I close my application
    > window. (I do not get "Change happened!" if I don't do my manual cp
    > command. :) )
    >
    > Personaly I believe this has something to do with the GUI/Qt4 part that I
    > have not understood. Something about how a widget repaint itself or
    > something in that direction.
    >
    > Best regards,
    > Joacim
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >



    You're right: I've found the following answer googling:

    http://mail.python.org/pipermail/python-list/2007-February/597617.html


    indeed, addind a startTimer() to your code, makes it works ;).


    Regards
    Marco

    --
    Marco Bizzarri
    http://notenotturne.blogspot.com/
    http://iliveinpisa.blogspot.com/
    Marco Bizzarri, Apr 26, 2009
    #2
    1. Advertising

  3. On Sun, 26 Apr 2009 14:46:14 +0200, Marco Bizzarri
    <> wrote:
    > On Sun, Apr 26, 2009 at 12:38 PM, Joacim Thomassen
    > <> wrote:
    >> Den Sat, 25 Apr 2009 23:47:57 +0200, skrev Marco Bizzarri:
    >>
    >>
    >> Hello Marco,
    >>
    >> python's fcntl() call the regular C fcntl() function and as stated in

    the
    >> manual pages for C fcntl:
    >>
    >> ----------- Snippet from fcntl man pages ----------------
    >>
    >> File and directory change notification (dnotify)
    >>       F_NOTIFY (long)
    >>              (Linux  2.4 onwards) Provide notification when the
    >>                      directory referred to by fd or any of

    the
    >> files that it
    >>                      contains is changed.  The  events  to
    >>  be  notified  are
    >>              specified in arg, which is a bit mask specified by
    >> ORing
    >>                      together zero or more of the following
    >> bits:
    >>
    >>              DN_MODIFY   A file was modified (write, pwrite,
    >> writev,
    >>                                                
    >>       truncate, ftruncate).
    >> ------------- End snippet of fcntl man pages -------------------
    >>
    >> The fact that my program actually trigger a signal as the monitored
    >> directoy's image.jpg file change confirm that this part of the code do
    >> work. I do get "Change happened!" as i manually do a "cp another.jpg
    >> image.jpg", but this action is first seen after I close my application
    >> window. (I do not get "Change happened!" if I don't do my manual cp
    >> command. :) )
    >>
    >> Personaly I believe this has something to do with the GUI/Qt4 part that

    I
    >> have not understood. Something about how a widget repaint itself or
    >> something in that direction.
    >>
    >> Best regards,
    >> Joacim
    >> --
    >> http://mail.python.org/mailman/listinfo/python-list
    >>

    >
    >
    > You're right: I've found the following answer googling:
    >
    > http://mail.python.org/pipermail/python-list/2007-February/597617.html
    >
    >
    > indeed, addind a startTimer() to your code, makes it works ;).


    Alternatively, use QFileSystemWatcher instead.

    Phil
    Phil Thompson, Apr 26, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pradnyesh Sawant

    passing tuple with pyqt4 signal/slot mechanism

    Pradnyesh Sawant, Apr 26, 2007, in forum: Python
    Replies:
    2
    Views:
    327
    Phil Thompson
    Apr 26, 2007
  2. Pradnyesh Sawant

    pyqt4 signal/slot using PyObject* and shortcut

    Pradnyesh Sawant, Apr 27, 2007, in forum: Python
    Replies:
    0
    Views:
    314
    Pradnyesh Sawant
    Apr 27, 2007
  3. wgw
    Replies:
    1
    Views:
    461
  4. nusch
    Replies:
    3
    Views:
    1,446
    Jeremy Sanders
    Oct 22, 2009
  5. Andrew
    Replies:
    3
    Views:
    1,170
    Andrew
    Oct 26, 2010
Loading...

Share This Page