简体   繁体   English

从 QDialog 2 关闭和打开 QDialog

[英]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:因此,我在 2 个不同的类中有两个 QDialog,在一个 Qdialog 中,我有一个 QTableWidget,其中包含来自 MYSQL 数据库的一些数据,我想在关闭第二个 Qdialog 时关闭并重新打开它,所以这是我尝试过的,但没有不行:

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()

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.实现此目的的一种方法是在启动第二个对话框时隐藏第一个对话框,然后让第二个对话框在其closeEvent期间发出一个关闭信号,该信号可以连接到第一个对话框中的插槽,使其在收到后再次可见。

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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM