[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: [PyKDE] Run function in the background
From:       Tom Brown <tom () nextstate ! net>
Date:       2007-02-19 18:57:13
Message-ID: 1171911433.2158.6.camel () chi
[Download RAW message or body]

On Mon, 2007-02-19 at 19:11 +0100, Fabian Steiner wrote:
> Hello!
> 
> I am currently working on an application which controls several hosts in 
> our network in order to ensure that there are online. This is done by a 
> function called checkStatus() that needs to be called every 10 seconds. 
> I tried the following (with checkStatus being called in __init__ as 
> thread.start_new_thread(self.checkStatus, ()), but it did't work since 
> it resulted in a segmentation fault:
> 
> 
> [...]
> checkStatus(self):
>      while 1:
>             if check_online('192.168.0.100'):
>                 self.ui.lblStatus.setText('ON')
>                 self.ui.btnOn.setEnabled(False)
>                 self.ui.btnOff.setEnabled(True)
>             else:
>                 self.ui.lblStatus.setText('OFF')
>                 self.ui.btnOn.setEnabled(True)
>                 self.ui.btnOff.setEnabled(False)
>             time.sleep(10)
> 
> Output:
> fabi@einstein:~/dev/Server_Control$ python Server_Control.py
> QObject: Cannot create children for a parent that is in a different thread.
> QObject: Cannot create children for a parent that is in a different thread.
> Segmentation fault
> 
> Is there any other way which would solve this issue?

You cannot update UI elements directly from a thread. I believe you
should use qApp.postEvent(). You can post events to the main window
which you catch in customEvent(). You have to override customEvent() in
your main window class. You probably want to use a QThread instead of a
python thread. I had better luck using QThread in my production
application.

HTH,
Tom

_______________________________________________
PyKDE mailing list    PyKDE@mats.imk.fraunhofer.de
http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic