views:

109

answers:

1

can i connect two objects that are in different classes ?

lets say i want button1's clicked() signal to clear line2

class A(QGroupBox):
    def __init__(self, parent=None):
        super(A, self).__init__(parent)
        self.button1= QPushButton('bt1')
        self.button1.show()

class B(QGroupBox):
    def __init__(self, parent=None):
        super(B, self).__init__(parent)
        self.line2 = QLineEdit()
        self.line2.show()

ob1 = A()
ob2 = B()
+1  A: 

Yes, create a method in object B that's tied to a signal in object A. Note how connect is called (this is just an example):

    self.connect(self.okButton, QtCore.SIGNAL("clicked()"),
                 self, QtCore.SLOT("accept()"))

The third argument is the object with the slot, and the fourth the slot name. The sending and receiving objects can definitely be different.

Eli Bendersky
def obconnect(self, ob2): self.connect(self.button1, SIGNAL('clicked()'), ob2.line, SLOT('clear()') )ob1.obconnect(ob2)u meant something like this ?
redouane
@redouane: almost. I don't recommend directly accessing attributes of ob2 from ob1 - it's better to encapsulate this interaction in a method of ob2 itself
Eli Bendersky