Scope <or> PyQt question

D

dittonamed

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)
 
S

Stargaming

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.
 
D

dittonamed

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.- Hide quoted text -

- Show quoted text -

I was having trouble getting that to work and thought it because of
event driven nature of gui/qt programming. But i'll give it another go
anyway .. any examples? ;-)

Im wondering what the "right" way to do this is - the Qt way, or is
what you mentioned in fact the "right" way? Can i access the QObject
another way or am i barking up the wrong tree?

Thanks Stargaming and to anyone else who has some input!

Ben
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top