pythonpyqt5qtablewidgetqdialog

Close and Open QDialog from QDialog 2


So, I have two QDialogs in 2 different classes, in one Qdialog I have a QTableWidget with some data from a MYSQL db, I want to close and re-open it when I close the second Qdialog, so this is what I tried but doesn't work:

class firstDialog(QtWidgets.QDialog):
   *all my code*
class secondDialog(QtWidgets.QDialog):
    def firstdial(self):
        self.firstdial= firstDialog()
        self.firstdial.show()
    def closeandopen(self)
        self.accept()
        firstDialog.accept()
        self.firstdial()

Solution

  • One way to achieve this would be to hide the first dialog upon launching the second dialog, and then having the second dialog emit a closing signal during it's closeEvent that can be connected to a slot in the first dialog that makes it visible again once received.

    I am extending the example used in one your previous question. I included some inline notes to explain where the changes are and what they are doing.

    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    def populate_table(table):
        for i in range(10):
            table.insertRow(table.rowCount())
            for j in range(2):
                item = QTableWidgetItem(type=0)
                item.setText(f"({i}, {j})")
                table.setItem(i,j,item)
    
    class Dialog2(QDialog):
    
        tableInfoChanged = pyqtSignal([int, int, str])
        closing = pyqtSignal()
    
        def __init__(self,parent=None):
            super().__init__(parent=parent)
            self.layout = QVBoxLayout()
            self.setLayout(self.layout)
            self.table = QTableWidget()
            self.table.setColumnCount(2)
            self.layout.addWidget(self.table)
            populate_table(self.table)
            self.table.cellChanged.connect(self.emitChangedInfo)
    
        def emitChangedInfo(self, row, col):
            text = self.table.item(row, col).text()
            self.tableInfoChanged.emit(row, col, text)
    
        def closeEvent(self, event):  # override the close event
            self.closing.emit()       # emit closing signal
            super().closeEvent(event) # let normal close event continue
    
    
    class Dialog1(QDialog):
        def __init__(self,parent=None):
            super().__init__(parent=parent)
            self.layout = QVBoxLayout()
            self.setLayout(self.layout)
            self.table = QTableWidget()
            self.table.setColumnCount(2)
            self.button = QPushButton("Push to open dialog2")
            self.layout.addWidget(self.table)
            self.layout.addWidget(self.button)
            populate_table(self.table)
            self.button.clicked.connect(self.openDialog2)
    
        def updateTable(self, row, col, text):
            self.table.item(row,col).setText(text)
    
        def openDialog2(self):
            self.dialog2 = Dialog2()
            self.dialog2.tableInfoChanged.connect(self.updateTable)
            # connect to the closing signal for second dialog
            self.dialog2.closing.connect(lambda: self.setVisible(True))  
            self.setVisible(False)  # Hide the first dialog
            self.dialog2.open()     # Launch second dialog
    
    
    app = QApplication(sys.argv)
    window = Dialog1()
    window.show()
    sys.exit(app.exec_())