PyQt Variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gregarican

    #1

    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.p y
    =============
    my_value = 'main'
    from child_window import *

    class Parent_Window(Q MainWindow):
    def __init__(self):
    QMainWindow.__i nit__(self,None ,'application main
    window',Qt.WDes tructiveClose)
    print my_value
    child_window= Child_Window()
    self.setCentral Widget(child_wi ndow)
    self.catchEvent ()

    child_window.py
    ============
    class Child_Window(QW idget):
    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.

  • Diez B. Roggisch

    #2
    Re: PyQt Variables

    gregarican schrieb:[color=blue]
    > 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.[/color]

    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

    Comment

    • gregarican

      #3
      Re: PyQt Variables

      Diez B. Roggisch wrote:
      [color=blue]
      > 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[/color]

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

      Comment

      Working...