首页 > 解决方案 > 如何从 QDialog 打开 QTableView

问题描述

我有一个显示登录表单的 QDialog。当用户输入详细信息并单击“确定”按钮时,我想关闭 QDialog 并打开一个 QTableView,其中包含用户输入的登录详细信息。

我试图将我的确定按钮连接到关闭我的 QDialog 并显示 QTableView 的函数,但我的 QTableView 出现半秒并且程序以“进程完成,退出代码 0”结束

谢谢 !

    class TableViewGUI(QAbstractTableModel):
    def __init__(self, user_id, mdp, profile):
    QAbstractTableModel.__init__(self)
    [...]

    class Dialog(QDialog):
    def __init__(self, parent=None):
         super(Dialog, self).__init__(parent)
         [...]

    self.button.accepted.connect(self.openTableView)

    def openTableView(self):
         #data input by the user in the QDialog form
         id = self.input_id.text()
         password = self.input_password.text()
         profile = str(self.comboBox_profile.currentText())

         model = TableViewGUI(id, password, profile)
         view = QTableView()
         view.setModel(model)
         self.close() #Close the QDialog
         view.show() #Open the QTableView

if __name__ == '__main__':
     app = QApplication(sys.argv)
     dialog = Dialog()
     dialog.show()
     sys.exit(app.exec_())

标签: pythonpyqt5qtableviewqdialog

解决方案


openTableView方法一结束,model和对象就view被销毁,因为它们只在方法的作用域内。self.在它们前面追加,如下所示:

    def openTableView(self):
         #data input by the user in the QDialog form
         id = self.input_id.text()
         password = self.input_password.text()
         profile = str(self.comboBox_profile.currentText())

         self.model = TableViewGUI(id, password, profile)
         self.view = QTableView()
         self.view.setModel(self.model)
         self.close() #Close the QDialog
         self.view.show() #Open the QTableView

推荐阅读