PYQT 3 communication with 2 windows

David Boddie david at boddie.org.uk
Fri Apr 20 20:43:58 EDT 2007


On Thursday 19 April 2007 22:38, Marcpp wrote:

> Hi, I'm introducing to program in python + pyqt.
> I have a main window that call a second window (to introduce a info
> with textedit)
> when press the second window button I need to return to the main
> window the info
> introduced in the second window.
> I've seek in the pyqt doc examples but i don't find it.
> Have you any example?

You could connect the button to a slot in the second window that sends
the text back to the first window.

Here's an example that sends the text to a function. You could substitute a
class for the function to get what you want.


import sys
from qt import *

class Window(QWidget):

    def __init__(self, parent = None):
    
        QWidget.__init__(self, parent)

        self.textEdit = QTextEdit(self)
        okButton = QPushButton(self.tr("&OK"), self)
        self.connect(okButton, SIGNAL("clicked()"), self.sendText)
        layout = QVBoxLayout(self)
        layout.addWidget(self.textEdit)
        layout.addWidget(okButton)
    
    def sendText(self):
    
        self.emit(PYSIGNAL("textEntered(QString)"), (self.textEdit.text(),))


def fn(text):

    print text

if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = Window()
    window.connect(window, PYSIGNAL("textEntered(QString)"), fn)
    window.show()
    app.setMainWidget(window)
    sys.exit(app.exec_loop())


Note the use of PYSIGNAL() instead of SIGNAL(). With PyQt4 you would be able
to use SIGNAL() and write the emit() call in a simpler form.

David



More information about the Python-list mailing list