473,320 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PyQt Variables

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.

Jan 5 '06 #1
2 5079
gregarican schrieb:
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/diveintopyt...ct_locals.html

for an introduction.

Regards,

Diez
Jan 5 '06 #2
Diez B. Roggisch wrote:
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/diveintopyt...ct_locals.html
for an introduction.
Regards,
Diez


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

Jan 5 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ilya Knizhnik | last post by:
Dear All, I am fairly new to Python. I have found it an easy language to learn and a very usefull one for many tasks. Currently I am working on a python project that involves a GUI and it was...
2
by: Srinath Avadhanula | last post by:
Hello, I am wondering if QT has something like QWaitForNextEvent() function. This function would block execution of the application till another key was pressed and then return the event...
35
by: Vamsi Mudrageda | last post by:
I am kind of new to Python, and after trying and using wxPython, I found it kind of lacking in easy-to-read documentation, speed at loading, and GUI response-time. So I am looking for an another...
8
by: simo | last post by:
OK, so I'm at the stage where I want to make a choice between wxPython and PyQt. Currently I'm using wxPython mainly due to the fact that it has GPL Linux and Windows versions. I prefer Qt to...
10
by: svenn.are | last post by:
Hi, I wanted to run a program that is written in PyQt on my mac, and went over to Riverbank to get PyQ 3.13 and SIP 4.1.1 sources. I followed the installation guide except for the compiling...
10
by: Michael McGarry | last post by:
Hi, I'm trying to get PyQt up and running under Mac OS X 10.2.8, but can't get past configuring sip. I installed fink's qt3-3.2.3-2 and restarted my shells to get qt3's environment variables...
10
by: Simon John | last post by:
After quite a while of wxPython I'm getting back into PyQt, mainly due to the announcement by Trolltech that they will make a GPL version of Qt4 for Windows (and Phil-T said he will make a PyQt to...
9
by: Eric Jardim | last post by:
Hi, Is there any site that gather all the documentation about PyQt? The docs of the Riverbank site is poor, and I have found separate tutorials on the net. I know that the Kompany have made...
3
by: Phil Thompson | last post by:
Riverbank Computing is pleased to announce the release of PyQt v4.0beta1 available from http://www.riverbankcomputing.co.uk/pyqt/. PyQt is a comprehensive set of Qt bindings for the Python...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.