[PyQt] Re: connection between 2 widget

David Boddie dboddie at trolltech.com
Mon Jun 22 19:01:27 BST 2009


On Sun Jun 21 17:37:54 BST 2009, Massimo Di Stefano wrote:

> I'm tring to get a solution,
> i searced for similar code in the pyqt examples source code
> but i can't find nothig similar.
>
> is the question i proposed comprensible?

It's a little difficult for me to understand, but I'll try and work through
it step by step:

> i can reproduce my problem with an example :
>
> W-1 (spinbox + button)   [ it is a main app, and has in the menu bar  
> an action to open W-2]

OK, I see this in your example.

> W-2 (line edit + button)

Right. I see this when I select the process->run menu item.

> i run W-1, pressing its button it increase the spinbox value,
> then pressing the action from the menu -> open W-2

Yes, these work as expected. A question: what happens if the user changes
the value in the spin box directly?

> W-2 , at its start, read value from W-1 -- pressing the W-2 button  
> process W-1 value and print it .

When I click the button, I see the value from the spin box in the line
edit.

> my problem :
>
> i need that everitime i change the W-1  value ...
> ... when i press button in W-2  it will process the W-1(changed value)

OK, so instead of passing a value when you create W-2, you want the value
to be taken from the spin box in W-1 and written to the line edit?

Here are some changes I made to your code to do what I think you want.

Firstly, I create the Elab widget in the init() method of your application
class instead of creating it later in the elaborazione() method. This makes
it possible for me to connect a signal from the ZoomSpinBox to a slot in
the widget.

[app.py]
[...]
    def init(self):
        self.w = GuiWidget()
        self.Value = 0
        self.query = Elab()
        self.connect(self.w.p1, SIGNAL("clicked()"), self.inc)
        self.connect(self.w.actionRun, SIGNAL("triggered()"),
                     self.elaborazione)
        self.connect(self.w.ZoomSpinBox, SIGNAL("valueChanged(double)"),
                     self.query.setValue)
        self.w.show()
[...]
    def elaborazione(self):
        self.query.show()


I just show the Elab widget when the elaborazione() method is called.

In the Elab class, we no longer need to pass an initial value, but you could
make the class take a value to begin with if you want. We don't want to show
the widget immediately, so I removed a call to its show() method.

[elab.py]

 class Elab(QWidget, Ui_Form):
    def __init__(self):
         QWidget.__init__(self)
         self.setupUi(self)
         self.value = 0
         self.connect(self.p2, SIGNAL("clicked()"),self.elabora)

     def elabora(self):
         newvalue = str(self.value)+str('-----')
         self.lineEdit.setText(newvalue)
         print newvalue

     def setValue(self, value):
         self.value = value

I added the setValue() slot that we used in the app.py file. This updates the
value held by this widget so that clicking the button causes an up-to-date
value to be used.

Is this what you had in mind?

David


More information about the PyQt mailing list