Scope <or> PyQt question

Stargaming stargaming at gmail.com
Thu Jul 26 16:15:51 CEST 2007


On Thu, 26 Jul 2007 06:41:44 -0700, dittonamed wrote:

> Code pasted below ----->
> 
> Can anyone out there suggest a way to access the object "p" thats
> started in playAudio() from inside the stopAudio()? I need the object
> reference to use QProcess.kill() on it. The code sample is below. Thanks
> to anyone who helps out =)
> 
> More comments in the code below  ------>
> 
> 
> from qt import *
> 
> class Form2(QMainWindow):
>     def __init__(self,parent = None,name = None,fl = 0):
>         QMainWindow.__init__(self,parent,name,fl) self.statusBar()
> 
>     def playAudio(self):
>         p = QProcess(self, 'player')
>         playcmd = '/usr/bin/play'
>         filename = 'song.ogg'
>         p.addArgument(playcmd)
>         p.addArgument(filename)
>         p.start()
> 
>     def stopAudio(self):
>         ''' #This is just to show that i can "see" the object, though
> i
>            #dont know how to "access" it
>            #the output shows the QProcess object by name... # but how do
>            i reference it??
>         allobjs = list(QObject.objectTrees()) for obj in allobjs:
>                 objName = QObject.name(obj)
>                 if objName == 'Form2':
>                         print QObject.children(obj)
>         '''
> 
>         QProcess.kill(NEED THE REFERENCE HERE)

Answering from a non-Qt point of view (ie. I don't know if there were 
cleaner ways using Qt stuff), you have to bind p somewhere not local to 
the function. Any attribute of `self` (that's hopefully not used by 
QMainWindow) should be fine.



More information about the Python-list mailing list