python - 如何从 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_())
解决方案
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
推荐阅读
- javascript - 使用 useEffect 响应 Redux 状态变化
- angular - 基于月份的Ionic 4嵌套firebase列表
- ruby - 编码挑战
- django - 如何修复“django.core.exceptions.ImproperlyConfigured:找不到 GDAL 库”
- sql - 在 SQL 中分组的累积计数
- javascript - 将动态文本从应用程序共享到 facebook - JavaScript SDK
- javascript - 如何使用 Winston 库在 Node.js 中每天将日志分隔到单独的文件中
- c# - 实体框架中的连接表C#高效方法
- raspberry-pi3 - 发现错误:错误:找不到模块“node_helper”
- javascript - 电子邮件的表单验证在 Vuejs 中不起作用