Connection between from QDialog to QMainWindow - PyQt5

1.9k views Asked by At

I have created two widgets (QMainWindow as win_one and QDialog as win_two) with qtdesigner and PyQt5. From win_one, I open win_two, fill-in the lineEdit and press OK to transfer the entry into a label displayed in win_one. Everything works well except two problems:

  1. win_one window is opened as .showMaximized() but after filled-in the label, the dimension of the window changes.
  2. the button from win_one stops to work

front_win_one.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_win_one(object):
    def setupUi(self, win_one):
        win_one.setObjectName("win_one")
        win_one.resize(1147, 234)
        self.centralwidget = QtWidgets.QWidget(win_one)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 50, 111, 51))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(40, 160, 131, 31))
        self.label.setObjectName("label")
        win_one.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, win_one):
        _translate = QtCore.QCoreApplication.translate
        win_one.setWindowTitle(_translate("win_one", "MainWindow"))
        self.pushButton.setText(_translate("win_one", "To qdialog"))
        self.label.setText(_translate("win_one", "TextLabel"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win_one = QtWidgets.QMainWindow()
    ui = Ui_win_one()
    ui.setupUi(win_one)
    win_one.show()
    sys.exit(app.exec_())

front_win_two.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_win_two(object):
    def setupUi(self, win_two):
        win_two.setObjectName("win_two")
        win_two.resize(317, 278)
        self.pushButton = QtWidgets.QPushButton(win_two)
        self.pushButton.setGeometry(QtCore.QRect(40, 120, 121, 23))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(win_two)
        self.lineEdit.setGeometry(QtCore.QRect(30, 50, 161, 21))
        self.lineEdit.setObjectName("lineEdit")

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

    def retranslateUi(self, win_two):
        _translate = QtCore.QCoreApplication.translate
        win_two.setWindowTitle(_translate("win_two", "Dialog"))
        self.pushButton.setText(_translate("win_two", "OK"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win_two = QtWidgets.QDialog()
    ui = Ui_win_two()
    ui.setupUi(win_two)
    win_two.show()
    sys.exit(app.exec_())

back.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QDialog
from front_win_1 import Ui_win_one
from front_win_2 import Ui_win_two

class win_two(QDialog, Ui_win_two):
    def __init__(self, parent=None):
        super(win_two, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.vers_main)

    def vers_main(self):
        entry = self.lineEdit.text()
        win_one().label.setText(entry)

class win_one(QMainWindow, Ui_win_one):
    def __init__(self, parent=None):
        super(win_one, self).__init__(parent)
        self.setupUi(dialog)
        self.pushButton.clicked.connect(self.open_qdialog)

    def open_qdialog(self):
        self.dialog_win_2 = win_two()
        self.dialog_win_2.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = QMainWindow()
    prog = win_one(dialog)
    dialog.showMaximized()
    sys.exit(app.exec_())

Thank you

1

There are 1 answers

0
eyllanesc On BEST ANSWER

Your code has some inconsistencies:

  1. You should not do this dialog = QMainWindow(), since it is enough to create an object of the class win_one, for this you must change self.setupUi(dialog) to self.setupUi(self).

  2. With the statement win_one().label.setText(entry) you are creating a new object, which is unnecessary, besides that you are losing the previous object so when you press the window again, QDialog is not opened, a simple solution is to pass it as parent to win_one to win_two and use the self.parent() function to access it.

All of the above is implemented in the following part:

class win_two(QDialog, Ui_win_two):
    def __init__(self, parent=None):
        super(win_two, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.vers_main)

    def vers_main(self):
        entry = self.lineEdit.text()
        self.parent().label.setText(entry)

class win_one(QMainWindow, Ui_win_one):
    def __init__(self, parent=None):
        super(win_one, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.open_qdialog)

    def open_qdialog(self):
        self.dialog_win_2 = win_two(self)
        self.dialog_win_2.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    prog = win_one()
    prog.showMaximized()
    sys.exit(app.exec_())

Note: I could never reproduce the first bug, only the second one.