Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: PyQt4 - widget signal trouble

Reply
Thread Tools

Re: PyQt4 - widget signal trouble

 
 
Marco Bizzarri
Guest
Posts: n/a
 
      04-25-2009
On Fri, Apr 24, 2009 at 7:37 PM, Joacim Thomassen
<(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
 
Marco Bizzarri
Guest
Posts: n/a
 
      04-26-2009
On Sun, Apr 26, 2009 at 12:38 PM, Joacim Thomassen
<(E-Mail Removed)> 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/pyt...ry/597617.html


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


Regards
Marco

--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
 
Reply With Quote
 
 
 
 
Phil Thompson
Guest
Posts: n/a
 
      04-26-2009
On Sun, 26 Apr 2009 14:46:14 +0200, Marco Bizzarri
<(E-Mail Removed)> wrote:
> On Sun, Apr 26, 2009 at 12:38 PM, Joacim Thomassen
> <(E-Mail Removed)> 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/pyt...ry/597617.html
>
>
> indeed, addind a startTimer() to your code, makes it works .


Alternatively, use QFileSystemWatcher instead.

Phil
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pyqt4 Table Widget deleting c/c++ object Andrew Python 3 10-26-2010 06:45 PM
PyQt4 - remember widget positions nusch Python 3 10-22-2009 09:22 AM
PyQt4.__file__ gives PyQt4/__init__.py as value wgw Python 1 08-15-2009 09:58 PM
pyqt4 signal/slot using PyObject* and shortcut Pradnyesh Sawant Python 0 04-27-2007 05:59 AM
passing tuple with pyqt4 signal/slot mechanism Pradnyesh Sawant Python 2 04-26-2007 03:05 PM



Advertisments