PDA

View Full Version : Sending Data Between Parent and Child Window



leomoon
15th August 2015, 21:54
Hello,

I'm practicing to make a multi window app and send data between them. I got the sending data from child to parent working but can't send data from parent to child. GUI python files are separate to make it easier to change the GUI later so there are 4 files in total:
main.py


# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
from mainGui import Ui_main
import one

class Main(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_main()
self.ui.setupUi(self)
self.ui.oneBtn.clicked.connect(one.One(self).show)
self.ui.getBtn.clicked.connect(self._get)
self.ui.sendBtn.clicked.connect(self._send)

def _get(self):
print()

def _send(self):
one.One.ui.edit.setText('blah')

def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

if __name__ == "__main__":
main()


mainGui.py


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainGui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_main(object):
def setupUi(self, main):
main.setObjectName(_fromUtf8("main"))
main.resize(281, 242)
main.setMinimumSize(QtCore.QSize(281, 242))
main.setMaximumSize(QtCore.QSize(281, 242))
self.centralwidget = QtGui.QWidget(main)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.edit = QtGui.QTextEdit(self.centralwidget)
self.edit.setGeometry(QtCore.QRect(10, 10, 261, 171))
self.edit.setObjectName(_fromUtf8("edit"))
self.oneBtn = QtGui.QPushButton(self.centralwidget)
self.oneBtn.setGeometry(QtCore.QRect(10, 190, 81, 41))
self.oneBtn.setObjectName(_fromUtf8("oneBtn"))
self.sendBtn = QtGui.QPushButton(self.centralwidget)
self.sendBtn.setGeometry(QtCore.QRect(190, 190, 81, 41))
self.sendBtn.setObjectName(_fromUtf8("sendBtn"))
self.getBtn = QtGui.QPushButton(self.centralwidget)
self.getBtn.setGeometry(QtCore.QRect(100, 190, 81, 41))
self.getBtn.setObjectName(_fromUtf8("getBtn"))
main.setCentralWidget(self.centralwidget)

self.retranslateUi(main)
QtCore.QMetaObject.connectSlotsByName(main)

def retranslateUi(self, main):
main.setWindowTitle(_translate("main", "Main Win", None))
self.oneBtn.setText(_translate("main", "One", None))
self.sendBtn.setText(_translate("main", "Send", None))
self.getBtn.setText(_translate("main", "Get", None))


one.py


# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from oneGui import Ui_one

class One(QtGui.QDialog):
def __init__(self,parent = None):
QtGui.QDialog.__init__(self, parent)
self.parent = parent
self.ui = Ui_one()
self.ui.setupUi(self)
self.ui.getBtn.clicked.connect(self._get)
self.ui.sendBtn.clicked.connect(self._send)

def _get(self):
self.ui.edit.setText(self.parent.ui.edit.toPlainTe xt())

def _send(self):
self.parent.ui.edit.setText(self.ui.edit.toPlainTe xt())


oneGui.py


# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'oneGui.ui'
#
# Created by: PyQt4 UI code generator 4.11.4
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_one(object):
def setupUi(self, one):
one.setObjectName(_fromUtf8("one"))
one.resize(201, 131)
one.setMinimumSize(QtCore.QSize(201, 131))
one.setMaximumSize(QtCore.QSize(201, 131))
self.edit = QtGui.QTextEdit(one)
self.edit.setGeometry(QtCore.QRect(10, 10, 181, 71))
self.edit.setObjectName(_fromUtf8("edit"))
self.getBtn = QtGui.QPushButton(one)
self.getBtn.setGeometry(QtCore.QRect(10, 90, 81, 31))
self.getBtn.setObjectName(_fromUtf8("getBtn"))
self.sendBtn = QtGui.QPushButton(one)
self.sendBtn.setGeometry(QtCore.QRect(110, 90, 81, 31))
self.sendBtn.setObjectName(_fromUtf8("sendBtn"))

self.retranslateUi(one)
QtCore.QMetaObject.connectSlotsByName(one)

def retranslateUi(self, one):
one.setWindowTitle(_translate("one", "Diag One", None))
self.getBtn.setText(_translate("one", "Get", None))
self.sendBtn.setText(_translate("one", "Send", None))

any help is appreciated.

leomoon
16th August 2015, 00:16
In the main.py, I changed _send() and _get(). It doesn't give any error but it doesn't do anything either:


def _get(self):
self.ui.edit.setText(one.One(self).ui.edit.toPlain Text())

def _send(self):
one.One(self).ui.edit.setText('blah')

anda_skoa
16th August 2015, 08:29
Instead of hacking into the parent, assuming private information in it (that is has a ui object and that this object has certain members), better provide accessor methods.

From a quick look you want to have a dialog with two buttons.
When you click the first button you want to get some data from the main window and display it.
When you click th eother button you want to take some data from the dialog and display it in the main window.

There are more or less two options:
1) Add two methods to the main window, where one gets the data you want and the other sets the data you want displayed in the main window
2) Add two methods to the dialog that can get and set the respective data and let the dialog signal the main window when it should call them

Cheers,
_

leomoon
20th August 2015, 01:19
I don't know if this is the right way of doing this but it works now. All other files are the same and only main.py is changed. This is an example of how to have multiple windows and send data back and forth:


# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui
from mainGui import Ui_main
import one

class Main(QtGui.QMainWindow):
one = None #make variable to hold the new window
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_main()
self.ui.setupUi(self)
self.one = one.One(self) #refrence the window
self.ui.oneBtn.clicked.connect(self.one.show)
self.ui.getBtn.clicked.connect(self._get)
self.ui.sendBtn.clicked.connect(self._send)

def _get(self):
self.ui.edit.setText(self.one.ui.edit.toPlainText( ))

def _send(self):
self.one.ui.edit.setText(self.ui.edit.toPlainText( ))

def main():
app = QtGui.QApplication(sys.argv)
main = Main()
main.show()
sys.exit(app.exec_())

if __name__ == "__main__":
main()