PyQt Variables

G

gregarican

I have an application I'm writing using PyQt. I'm trying to create the
various windows by subclassing Qt objects. I have a subclassed
QMainWindow as the parent, and then a series of subclassed QWidgets as
other child windows that get used. How can I pass variables back and
forth between the parent and child windows? For example, if I have a
child window that processes a customer lookup I want the retrieved
values to be passed back to the QMainWindow. I have tried using global
variables, but these variables seem to be global only in the sense of
each subclassed object. Not between the subclassed objects.

Here's a generic sample:

parent_window.py
=============
my_value = 'main'
from child_window import *

class Parent_Window(QMainWindow):
def __init__(self):
QMainWindow.__init__(self,None,'application main
window',Qt.WDestructiveClose)
print my_value
child_window= Child_Window()
self.setCentralWidget(child_window)
self.catchEvent()

child_window.py
============
class Child_Window(QWidget):
def __init__(self):
QWidget.__init__(self)
print my_value

This doesn' t seem to work, as the Child_Window class doesn't recognize
the my_value global variable that appears in the Parent_Window class.
 
D

Diez B. Roggisch

gregarican said:
I have an application I'm writing using PyQt. I'm trying to create the
various windows by subclassing Qt objects. I have a subclassed
QMainWindow as the parent, and then a series of subclassed QWidgets as
other child windows that get used. How can I pass variables back and
forth between the parent and child windows? For example, if I have a
child window that processes a customer lookup I want the retrieved
values to be passed back to the QMainWindow. I have tried using global
variables, but these variables seem to be global only in the sense of
each subclassed object. Not between the subclassed objects.

Looks (or better smells) like a design smell to me. In Qt,and especially
PyQt, you rarely subclass widgets, as that makes you lose the
possibility to use the fabulous designer. The only thing I subclass in
PyQt are the designer-generetaed top-level-classes. Can be a widget, or
sometimes dialogs.

QObjects usually communicate using signals/slots. That ensures a nice
weak coupling of components and enforces good MVC design. If you
absolutely must share state by means of instances of widgets,
explicietly introduce them to each other. No globals needed!

Additionally, what you experience is the fact that python only knows
module-global variables. See

http://www.faqs.org/docs/diveintopython/dialect_locals.html

for an introduction.

Regards,

Diez
 
G

gregarican

Diez said:
Looks (or better smells) like a design smell to me. In Qt,and especially
PyQt, you rarely subclass widgets, as that makes you lose the
possibility to use the fabulous designer. The only thing I subclass in
PyQt are the designer-generetaed top-level-classes. Can be a widget, or
sometimes dialogs.

QObjects usually communicate using signals/slots. That ensures a nice
weak coupling of components and enforces good MVC design. If you
absolutely must share state by means of instances of widgets,
explicietly introduce them to each other. No globals needed!


Additionally, what you experience is the fact that python only knows
module-global variables. See


http://www.faqs.org/docs/diveintopython/dialect_locals.html


for an introduction.


Regards,


Diez

Thanks for the reply. Makes perfect sense now that you point this out!
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top