Как обновить содержимое TableView,когда в базу данных занесены значения? [关闭]

问题描述 投票:0回答:0

我写了一个将数据输入到sql表中的函数。理论上,按我的理解,这个数据应该显示在TableView中,但是他们没有显示,尽管数据被输入到表中。我尝试了reset(), update() 方法来更新表,没有用,反正表是空的。应用重启后显示内容

class NewTransaction(QDialog):
    def __init__(self):
        super(NewTransaction, self).__init__()
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.db = DataBase()
        self.et = ExpensesTracker()

        self.ui.button_new_transaction.clicked.connect(self.add_new_transaction)

    def add_new_transaction(self):
        self.index = self.ui.cb_category.currentIndex()
        self.item = self.ui.cb_category.itemText(self.index)
        self.data = self.ui.data.text()
        self.ld = self.ui.le_description.text()
        self.lb = self.ui.le_balance.text()
        self.status_index = self.ui.comboBox_2.currentIndex()
        self.status = self.ui.comboBox_2.itemText(self.status_index)
        self.et.rec.setValue('category', self.item)
        self.et.rec.setValue('date', self.data)
        self.et.rec.setValue('descriptions', self.ld)
        self.et.rec.setValue('balance', self.lb)
        self.et.rec.setValue('status', self.status)
        self.et.transactions.insertRecord(-1, self.et.rec)
        self.et.ui.tableView.reset()
        self.ui.btn_add.clicked.connect(self.add_new_transaction)

        self.transactions = QSqlTableModel()
        self.transactions.setTable('transactions')
        self.transactions.editStrategy()
        self.transactions.select()
        self.rec = connect_db('transactions.db').record('transactions')
        self.ui.tableView.setModel(self.transactions)

    def add_new_transaction(self):
        self.other_window = NewTransaction()
        self.other_window.exec_()
    def connect_db(db_name):
        con = QSqlDatabase.addDatabase('QSQLITE')
        con.setDatabaseName(db_name)
        if not con.open():
            return False

        return con

    if not connect_db('transactions.db'):
        sys.exit(-1)
    else:
        print('Подключение прошло успешно')
pyqt pyqt5 pyside6
© www.soinside.com 2019 - 2024. All rights reserved.